diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/add_binary.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
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 +} |