Skip to content

Commit 7ec1a9e

Browse files
Add Binary(Plus / %)
1.next = Plus / 2; current = Plus % 2; 2.The sequence of '+' ; 3.res = str(Plus % 2) + res NOT equal res += str(Plus % 2)
1 parent db0ae71 commit 7ec1a9e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

067(Plus)_Add Binary.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def addBinary(self, a, b):
3+
"""
4+
:type a: str
5+
:type b: str
6+
:rtype: str
7+
"""
8+
res = ''
9+
i, j, plus = len(a) - 1, len(b) - 1, 0
10+
while i >= 0 or j >= 0 or 1 == plus: #process a & b simultaneously
11+
plus += int(a[i]) if i >= 0 else 0
12+
plus += int(b[j]) if j >= 0 else 0
13+
res = str(plus % 2) + res # != res+= str(plus % 2)
14+
i, j, plus = i - 1, j - 1, plus / 2
15+
return res
16+

0 commit comments

Comments
 (0)