67. add binary

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Description

Input: a = "11", b = "1"
Output: "100"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  {
public:
string addBinary(string a, string b) {
int alen=a.size()-1,blen=b.size()-1,c=0;
string ans="";
while(alen>=0||blen>=0||c==1)
{
c+=alen>=0?a[alen--]-'0':0;
c+=blen>=0?b[blen--]-'0':0;
ans=char(c%2+'0')+ans;
c/=2;
}
return ans;
}
};