Skip to content

Commit 69e10d3

Browse files
Create Add-Binary.cpp
1 parent ffb8518 commit 69e10d3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Add-Binary.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution
2+
{
3+
public:
4+
string addBinary(string a, string b)
5+
{
6+
string s = "";
7+
8+
int c = 0, i = a.size() - 1, j = b.size() - 1;
9+
while(i >= 0 || j >= 0 || c == 1)
10+
{
11+
c += i >= 0 ? a[i --] - '0' : 0;
12+
c += j >= 0 ? b[j --] - '0' : 0;
13+
s = char(c % 2 + '0') + s;
14+
c /= 2;
15+
}
16+
17+
return s;
18+
}
19+
};

0 commit comments

Comments
 (0)