-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd Binary.cpp
34 lines (34 loc) · 1.05 KB
/
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
class Solution {
public:
string addBinary(string a, string b) {
if(b.size() > a.size()) swap(a,b);
while(b.size() < a.size()) b = "0" + b;
int carry = 0;
string result = "";
for(int i = b.size()-1; i >= 0 ; --i)
{
if(b[i] == '1' && a[i]=='1')
{
if(carry == 0) result = "0" + result;
else result = "1" + result;
carry = 1;
}
else if(b[i] =='0' && a[i] =='0')
{
if(carry == 0) result = "0" + result;
else
{
result = "1" + result;
carry = 0;
}
}
else if((b[i]=='0' && a[i]=='1') || (b[i]=='1' && a[i] == '0'))
{
if(carry == 0) result = "1" + result;
else result = "0" + result;
}
}
if(carry == 1) result = "1" + result;
return result;
}
};