Add Binary LeetCode solution in C++

1
class Solution {
2
public:
3
string addBinary(string a, string b) {
4
string ans;
5
int i = a.length() - 1, j = b.length() - 1;
6
int carry = 0;
7
while (carry || i >= 0 || j >= 0) {
8
if (i >= 0) {
9
carry += a[i] - '0';
10
i--;
11
}
12
if (j >= 0) {
13
carry += b[j] - '0';
14
j--;
15
}
16
ans += carry % 2 + '0';
17
carry = carry / 2;
18
}
19
reverse(ans.begin(), ans.end());
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0