We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8d77d69 commit 2f14a6aCopy full SHA for 2f14a6a
30-days-of-code-july/19.addBinary.cpp
@@ -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