-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67.add-binary.python2.py
More file actions
53 lines (39 loc) · 1.01 KB
/
Copy path67.add-binary.python2.py
File metadata and controls
53 lines (39 loc) · 1.01 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
52
53
# @leet start
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a = list(a)[::-1]
b = list(b)[::-1]
if len(a) < len(b):
a, b = b, a
for i in range(len(b)):
if a[i] == "1" and b[i] == "1":
a[i] = "0"
j = 1
while j + i - 1 < len(a):
if j + i == len(a):
a.append("1")
break
elif a[i + j] == "1":
a[i + j] = "0"
j += 1
elif a[i + j] == "0":
a[i + j] = "1"
j += 1
break
elif a[i] == "0" and b[i] == "1":
a[i] = "1"
return "".join(a)[::-1]
sol = Solution()
# a = "11"
# b = "1"
# a = "1010"
# b = "1011"
a = "111"
b = "110000"
print(sol.addBinary(a, b))
# @leet end