-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddBinary.cpp
34 lines (31 loc) · 988 Bytes
/
addBinary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* https://leetcode.com/problems/add-binary/
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*/
class Solution {
public:
string addBinary(string a, string b) {
int carry = 0;
auto size_a = a.size();
auto size_b = b.size();
string res;
string zero(llabs(size_a - size_b), '0'); // padding zeroes
if(size_a < size_b)
a.insert(a.begin(), zero.begin(), zero.end());
else
b.insert(b.begin(), zero.begin(), zero.end());
for(int i = max(size_a, size_b)-1; i>=0; i--){
auto tmp = carry + (a[i]-'0') + (b[i]-'0'); // be care of the conversion
carry = tmp/2;
res.insert(res.begin(), tmp%2 + '0'); // caution: don't forget to convert to char
}
if(carry == 1)
res.insert(res.begin(), '1');
if(res.empty())
res.push_back('0');
return res;
}
};