-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-binary.cpp
More file actions
69 lines (54 loc) · 1.3 KB
/
add-binary.cpp
File metadata and controls
69 lines (54 loc) · 1.3 KB
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
68
69
//question-67//
Given two binary strings a and b, return their sum as a binary string.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
Constraints:
1 <= a.length, b.length <= 104
a and b consist only of '0' or '1' characters.
Each string does not contain leading zeros except for the zero itself.
//solution//
class Solution {
public:
string addBinary(string a, string b) {
int n1 = a.size();
int n2 = b.size();
int carry = 0;
int i = n1-1;
int j = n2-1;
string s = "";
while(i>=0 || j>=0)
{
int ad,bd;
if(i>=0)
{
ad = a[i]-'0';
}
else
{
ad = 0;
}
if(j>=0)
{
bd = b[j]-'0';
}
else
{
bd = 0;
}
int sum = ad + bd + carry;
carry = sum/2;
sum = sum%2;
s = s + to_string(sum);
i--;
j--;
}
if(carry==1)
s = s + to_string(carry);
reverse(s.begin(),s.end());
return s;
}
};