forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
067 Add Binary.py
58 lines (51 loc) · 1.49 KB
/
067 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
__author__ = 'Danyang'
class Solution:
def addBinary_builtin(self, a, b):
"""
Built-in function
:param a: string
:param b: string
:return: string
"""
a = int(a, 2)
b = int(b, 2)
return bin(a+b)[2:]
def addBinary(self, a, b):
"""
Built-in function
:param a: string
:param b: string
:return: string
"""
if len(a)>len(b):
a, b = b, a
a, b = list(a), list(b)
# from LSB to MSB
a.reverse()
b.reverse()
# b as the base number
for i in xrange(len(a)):
if a[i]=="0": # 0
continue
elif b[i]=="0": # 0+1
b[i] = "1"
continue
else: # 1+1
b[i] = "0"
# carry forward
if i==len(b)-1:
b.append("1")
else:
for j in range(i+1, len(b)):
if b[j]=="0":
b[j] = "1"
break
else:
b[j] = "0" # carry forward
if j==len(b)-1:
b.append("1")
break
b.reverse()
return "".join(b) # reversed back
if __name__=="__main__":
print Solution().addBinary("11", "1")