-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd_binary_numbers.cpp
67 lines (45 loc) · 1.19 KB
/
add_binary_numbers.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Given two strings a and b that represent binary numbers,
add them and return their sum, also as a string.
The input strings are guaranteed to be non-empty and contain only 1s and 0s.
Constraints
n = 100,000 where n is the length of a
m = 100,000 where m is the length of b
Example 1
Input
a = "1"
b = "1"
Output
"10"
Example 2
Input
a = "111"
b = "1"
Output
"1000"
// TC -> O(max(m,n)), where m = length of string a, and n = length of string b
// SC -> O(1) We use just constant auxiliary space.
string solve(string a, string b) {
int i = a.length() - 1, j = b.length() - 1, carry = 0;
string addition = "";
while (i >= 0 || j >= 0) {
int sum = 0;
if (i >= 0) sum += (a[i--] - '0');
if (j >= 0) sum += (b[j--] - '0');
sum += carry;
addition += ((sum % 2) + '0');
carry = sum / 2;
}
if (carry) {
addition += '1';
}
reverse(addition.begin(), addition.end());
// remove starting zeros
if (addition.length() > 1) {
int k;
for (k = 0; k < addition.length(); k++) {
if (addition[k] != '0') break;
}
addition = addition.substr(k);
}
return addition;
}