class Solution {
public:
string addBinary(string a, string b) {
string ans="",res="";
int n=a.size();
int m=b.size();
if(!n||!m)
return ans;
int i,j;
int cnt=0;
for(i=n-1,j=m-1;i>=0||j>=0;i--,j--)
{
int tmp=cnt;
if(i>=0)
tmp+=a[i]-'0';
if(j>=0)
tmp+=b[j]-'0';
ans=to_string(tmp%2)+ans;
cnt=tmp/2;
}
if(cnt)
ans=to_string(cnt)+ans;
return ans;
}
};
718

被折叠的 条评论
为什么被折叠?



