-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67.py
47 lines (47 loc) · 1.4 KB
/
67.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
class Solution:
def addBinary(self, a: str, b: str) -> str:
if len(a)>len(b):
a, b = b, a
c = 0
d = ['0' for i in range(len(b)+1)]
for i in range(-1, -len(a)-1, -1):
print('i=:',i)
print(a[i],b[i],c)
if int(a[i]) + int(b[i]) + c == 0:
d[i] = '0'
c = 0
print('a')
elif int(a[i]) + int(b[i]) + c == 1:
d[i] = '1'
c = 0
print('b')
print(d)
elif int(a[i]) + int(b[i]) + c == 2:
d[i] = '0'
c = 1
print('c')
print(d)
elif int(a[i]) + int(b[i]) + c == 3:
# print('wrong')
# print(int(a[i]),int(b[i]),c)
d[i] = '1'
c = 1
print('d')
# print(c)
for i in range(-len(a)-1, -len(b)-1, -1):
if int(b[i]) + c == 0:
d[i] = '0'
c = 0
if int(b[i]) + c == 1:
d[i] = '1'
c = 0
if int(b[i]) + c == 2:
d[i] = '0'
c = 1
# print(c)
if c == 1:
d[0] = '1'
if d[0] == '0':
return ''.join(d[1:])
else:
return ''.join(d)