还是模拟,模拟就完事儿了
class Solution {
public:
string addBinary(string a, string b) {
int s_it_a=a.length()-1;
int s_it_b=b.length()-1;
string res="";
int carry=0;
while(s_it_a>=0||s_it_b>=0){
int c_a=s_it_a>=0?a[s_it_a]-'0':0;
int c_b=s_it_b>=0?b[s_it_b]-'0':0;
int c_k=c_a+c_b+carry;
carry=0;
if(c_k>1){
carry=1;
c_k=c_k-2;
}
res=(char)('0'+c_k)+res;
--s_it_a;
--s_it_b;
}
while(carry){
res='1'+res;
--carry;
}
return res;
}
};