Leetcode 67 Solution
This article provides solution to leetcode question 67 (add-binary)
Access this page by simply typing in "lcs 67" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/add-binary
Solution
class Solution {
public:
string addBinary(string a, string b) {
string c;
auto it1 = a.rbegin();
auto it2 = b.rbegin();
int carry = 0;
while (it1 != a.rend() || it2 != b.rend())
{
int val = carry;
if (it1 != a.rend())
{
if ((*it1) == '1')
val++;
it1++;
}
if (it2 != b.rend())
{
if ((*it2) == '1')
val++;
it2++;
}
if (val >= 2)
{
val -= 2;
carry = 1;
}
else
carry = 0;
c += to_string(val);
}
if (carry == 1)
c += "1";
reverse(c.begin(), c.end());
return c;
}
};