-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_binary.py
More file actions
51 lines (44 loc) · 1.11 KB
/
add_binary.py
File metadata and controls
51 lines (44 loc) · 1.11 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
48
49
50
51
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
r = list()
a1 = list(a)[::-1]
b1 = list(b)[::-1]
leftover = a1 if len(a1) > len(b1) else b1
min = len(a1) if len(a1) < len(b1) else len(b1)
max = len(a1) if len(a1) > len(b1) else len(b1)
i = 0
c = 0
while i < min:
s = int(a1[i]) + int(b1[i]) + c
if s == 3:
r.append('1')
c = 1
elif s == 2:
r.append('0')
c = 1
else:
r.append(str(s))
c = 0
i = i + 1
while i < max:
s = int(leftover[i]) + c
if s == 2:
r.append('0')
c = 1
else:
r.append(str(s))
c = 0
i = i + 1
if c == 1:
r.append('1')
r = r[::-1]
return "".join(r)
if __name__ == "__main__":
s = Solution()
r = s.addBinary("1", "111")
print(r)