Skip to content

Commit

Permalink
Time: 13 ms (11.10%), Space: 6.3 MB (56.88%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Monalisa0311 committed Nov 14, 2022
1 parent 71ebf81 commit cd41e35
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 0067-add-binary/0067-add-binary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
string addBinary(string a, string b) {
string res;
int i=a.length()-1;
int j=b.length()-1;
int carry=0;
while(i>=0 || j>=0)
{
int sum=carry;
if(i>=0)
sum+=a[i--]-'0';
if(j>=0)
sum+=b[j--]-'0';
carry=(sum>1)?1:0;
res+=to_string(sum%2);
}
if(carry)
res+=to_string(carry);
reverse(res.begin(),res.end());
return res;
}
};

0 comments on commit cd41e35

Please sign in to comment.