Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
class Solution {
public:
string addBinary(string a, string b) {
int carry = , alen = a.length(),blen = b.length();
string res="";
if(alen > blen) {b=string(alen-blen,'')+b;blen = alen;}
if(alen < blen) {a=string(blen-alen,'')+a;alen = blen;}
for(int i = alen-; i>= ; -- i){
int num =(a[i]-'')+(b[i]-'')+carry;
carry = ;
if(num >= ) {num-=;carry=;}
res+=''+num;
}
if(carry) res+='';
reverse(res.begin(),res.end());
return res;
}
};