Skip to content

Commit 2f14a6a

Browse files
Added day20 solution
1 parent 8d77d69 commit 2f14a6a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

30-days-of-code-july/19.addBinary.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string addBinary(string a, string b) {
4+
int la= a.size();
5+
int lb=b.size();
6+
string res="";
7+
int carry=0;
8+
while(la>0 || lb >0)
9+
{
10+
int sum = carry;
11+
if(la > 0)
12+
sum += (a[--la] - '0');
13+
14+
if(lb > 0)
15+
sum += (b[--lb] - '0');
16+
17+
res.insert(res.begin(), sum % 2 + '0');
18+
carry = sum / 2;
19+
}
20+
if(carry > 0)
21+
res.insert(res.begin(), '1');
22+
23+
return res;
24+
}
25+
};

0 commit comments

Comments
 (0)