Add Binary
code
class Solution {
public:
string addBinary(string a, string b) {
string ret;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string ar = a;
string br = b;
int i = 0;
int j = 0;
int carry = 0;
while(i < ar.length() && j < br.length())
{
int res = int(ar[i]) - '0' + int(br[j]) - '0' + carry;
carry = res/2;
res = res%2;
ret += to_string(res);
i++;
j++;
}
while(i < ar.length())
{
int res = int(ar[i]) - '0' + carry;
carry = res/2;
res = res%2;
ret += to_string(res);
i++;
}
while(j < br.length())
{
int res = int(br[j]) - '0' + carry;
carry = res/2;
res = res%2;
ret += to_string(res);
j++;
}
if(carry == 1)
{
ret += "1";
}
reverse(ret.begin(), ret.end());
return ret;
}
};
```
``