diff options
Diffstat (limited to 'other/add_binary.cpp')
-rw-r--r-- | other/add_binary.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/other/add_binary.cpp b/other/add_binary.cpp new file mode 100644 index 0000000..9afc1dc --- /dev/null +++ b/other/add_binary.cpp @@ -0,0 +1,30 @@ +// 67. Add Binary +#include "leetcode.h" + +class Solution { +public: + string addBinary(string a, string b) { + int i = a.size() - 1, j = b.size() - 1, carry = 0; + string ans; + while (i >= 0 || j >= 0 || carry) { + if (i >= 0) { + carry += a[i] - '0'; + i--; + } + if (j >= 0) { + carry += b[j] - '0'; + j--; + } + ans += (carry % 2 + '0'); + carry /= 2; + } + reverse(ans.begin(), ans.end()); + return ans; + } +}; + +int main() { + Solution obj; + cout << obj.addBinary("11", "1") << endl; // expect: 100 + cout << obj.addBinary("1010", "1011") << endl; // expect: 10101 +} |