problems:
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) { string result; int n=a.size()>b.size()?a.size():b.size(); //确定边界 reverse(a.begin(),a.end()); //字符串加法不用倒着来 reverse(b.begin(),b.end()); int carry=0; //用来表进位 for(int i=0;i<n;i++) { int ai=i<a.size()?a[i]-'0':0; int bi=i<b.size()?b[i]-'0':0; int val=(ai+bi+carry)%2; carry=(ai+bi+carry)/2; result.insert(result.begin(),val+'0'); } if(carry==1) //相加最多多一位 result.insert(result.begin(),'1'); return result; } };