https://leetcode.com/problems/add-binary/description/
string addBinary(string a, string b) {
if(a.size()<b.size()) return addBinary(b, a);
int c = 0;
int j = b.size()-1, i = a.size()-1;
for(;i>=0 && (c||j>=0); i--,j--,c/=2) {
c += a[i] - '0' + (j>=0? b[j] - '0':0);
a[i] = c%2 + '0';
}
return (c? "1":"") + a;
}
No comments:
Post a Comment