-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67_Add_Binary.py
More file actions
47 lines (46 loc) · 1.17 KB
/
67_Add_Binary.py
File metadata and controls
47 lines (46 loc) · 1.17 KB
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
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
la = len(a)
lb = len(b)
index = 0
s = ''
if la > lb:
b = '0'*(la-lb) + b
else:
a = '0'*(lb-la) + a
a = a[::-1]
b = b[::-1]
for i in range(len(a)):
if index == 0:
if a[i] == '1' and b[i] == '1':
s += '0'
index = 1
elif a[i] == '0' and b[i] == '0':
s += '0'
index = 0
else:
s += '1'
index = 0
else: #index == 1
if a[i] == '1' and b[i] == '1':
s += '1'
index = 1
elif a[i] == '0' and b[i] == '0':
s += '1'
index = 0
else:
s += '0'
index = 1
if index == 1:
s += '1'
return s[::-1]
if __name__ == '__main__':
a = "11"
b = "1"
c = Solution()
print c.addBinary(a, b)