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-06-23 10:35:54 发布