Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using std::string;
- string addBinary(string a, string b) {
- const string& max = a.length() > b.length() ? a : b;
- const string& min = max == a ? b : a;
- char* result_buffer = new char[max.length() + 2];
- memset(result_buffer, 0, max.length() + 2);
- int insert_index = max.length();
- bool extra = false;
- for (int i = max.length() - 1, j = min.length() - 1; i >= 0; i--, j--)
- {
- int current = (max[i] - '0') + (j >= 0 ? min[j] - '0' : 0) + (extra ? 1 : 0);
- extra = current >= 2;
- current %= 2;
- result_buffer[insert_index--] = '0' + current;
- }
- char* res_pointer = result_buffer;
- if (extra)
- {
- res_pointer[insert_index] = '1';
- }
- else
- {
- res_pointer++;
- }
- string result(res_pointer);
- delete[] result_buffer;
- return result;
- }
- bool isPalindrome(int x)
- {
- if (x < 0)
- {
- return false;
- }
- char buffer[11];
- memset(buffer, 0, 11);
- int insertIndex = 0;
- while (x > 0)
- {
- buffer[insertIndex++] = x % 10 + '0';
- x /= 10;
- }
- for (int i = 0, j = insertIndex - 1; i < j; i++, j--)
- {
- if (buffer[i] != buffer[j])
- {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement