-
Notifications
You must be signed in to change notification settings - Fork 0
/
67.py
52 lines (47 loc) · 1.09 KB
/
67.py
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
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
i = len(a)-1
j = len(b)-1
ret = []
flag = 0
while i>=0 and j>=0:
s = int(a[i])+int(b[j])+flag
if s>1:
s = s%2
flag = 1
else:
flag=0
ret.append(s)
i-=1
j-=1
while i>=0:
s = int(a[i])+flag
if s > 1:
s = s % 2
flag = 1
else:
flag=0
ret.append(s)
i -= 1
while j>=0:
s = int(b[j])+flag
if s > 1:
s = s % 2
flag = 1
else:
flag=0
ret.append(s)
j -= 1
if flag > 0:
ret.append(flag)
ret.reverse()
ret = map(lambda x:str(x), ret)
return ''.join(ret)
if __name__ == '__main__':
solution = Solution()
print solution.addBinary('1010', '1011')