class Solution {
public:
string addBinary(string a, string b) {
string res;
int i=a.size()-1,j=b.size()-1;
int carry=0;
while(i>=0&&j>=0)
{
int temp=a[i]-'0'+b[j]-'0'+carry;
res+=temp%2+'0';
carry=temp/2;
i--;
j--;
}
while(i>=0)
{
int temp=a[i]-'0'+carry;
res+=temp%2+'0';
carry=temp/2;
i--;
}
while(j>=0)
{
int temp=b[j]-'0'+carry;
res+=temp%2+'0';
carry=temp/2;
j--;
}
if(carry)
res+=carry+'0';
reverse(res.begin(),res.end());
return res;
}
};
leetcode 67: Add Binary
最新推荐文章于 2020-05-29 00:06:18 发布