-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_0067_add_binary__Carry.py
42 lines (41 loc) · 1.13 KB
/
leet_0067_add_binary__Carry.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
class Solution:
def addBinary(self, a: str, b: str) -> str:
la = len(a)
lb = len(b)
lmax = max(la, lb)
diff = abs(la - lb)
E = ''
s = '0' * diff
if la != lmax:
a = s + a
if lb != lmax:
b = s + b
# print(a, b, len(a), len(b), sep='\n')
for i in range(lmax):
E += str(int(a[i]) + int(b[i]))
# print(E, E[::-1], sep='\n')
carry = 0
res = ''
for c in E[::-1]:
if c == '0':
if carry == 0:
res += '0'
else:
res += '1'
carry -= 1
elif c == '1':
if carry == 0:
res += '1'
else:
res += '0'
elif c == '2':
if carry == 0:
carry += 1
res += '0'
else:
res += '1'
# print(res, carry)
if carry != 0:
for _ in range(carry):
res += '1'
return res[::-1]