forked from Aniket123cloud/hacktoberfestani-2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67. Add Binary.cpp
47 lines (43 loc) · 1.34 KB
/
67. Add Binary.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Add Binary.
//Memory Usage: 8.6 MB, less than 61.30% of C++ online submissions for Add Binary.
class Solution {
public:
string addBinary(string a, string b) {
int N = max(a.size(), b.size()) + 1;
//padding
a.insert(a.begin(), N-a.size(), '0');
b.insert(b.begin(), N-b.size(), '0');
// cout << a << endl;
// cout << b << endl;
//now we can iterate from i = 0
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
// cout << a << endl;
// cout << b << endl;
int i = 0, carry = 0;
//add b to a
for(int i = 0; i < N; i++){
int ai = a[i]-'0', bi = b[i]-'0';
int sumi = ai + bi + carry;
switch(sumi){
case 3:
a[i] = '1';
carry = 1;
break;
case 2:
a[i] = '0';
carry = 1;
break;
case 1:
a[i] = '1';
carry = 0;
break;
}
}
// cout << a << endl;
// cout << b << endl;
if(a[N-1] == '0') a.resize(N-1);
reverse(a.begin(), a.end());
return a;
}
};