forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
35 lines (35 loc) · 768 Bytes
/
solution.cpp
File metadata and controls
35 lines (35 loc) · 768 Bytes
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
class Solution
{
public:
string addBinary(string a, string b)
{
string c;
int flag=0;
int lena = a.size();
int lenb = b.size();
int len = abs(lena-lenb);
string append(len,'0');
if(lena>lenb)
{
b = append + b;
c.resize(lena,'0');
}
else
{
a = append + a;
c.resize(lenb,'0');
}
for(int j=c.size()-1;j>=0;j--)
{
int current = (a[j]-'0') ^(b[j]-'0') ^flag;
if((a[j]-'0') +(b[j]-'0') +flag >1)
flag = 1;
else
flag = 0;
c[j] = current+'0';
}
if(flag == 1)
c = '1'+ c;
return c;
}
};