-
Notifications
You must be signed in to change notification settings - Fork 0
/
67_Add_Binary.py
33 lines (32 loc) · 894 Bytes
/
67_Add_Binary.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
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
if(len(a) > len(b)):
list1 = list(a)
list2 = list(b)
else:
list1 = list(b)
list2 = list(a)
result = []
# list1 refer to the longer one
flag = 0
for i in range(len(list1)):
if(i < len(list2)):
temp = int(list1[len(list1)-1-i]) + int(list2[len(list2)-1-i]) + flag
else:
temp = int(list1[len(list1)-1-i]) + flag
if(temp >= 2):
temp = temp - 2
flag = 1
else:
flag = 0
result = [str(temp)] + result
if(flag):
result = [str(flag)] + result
else:
pass
return ''.join(result)