-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0067_Add_Binary.py
More file actions
60 lines (54 loc) · 1.59 KB
/
0067_Add_Binary.py
File metadata and controls
60 lines (54 loc) · 1.59 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
54
55
56
57
58
59
60
class Solution:
def addBinary(self, a: str, b: str) -> str:
# Time: O(B)
# Space: O(B)
if a[0] == '0':
return b
if b[0] == '0':
return a
A = len(a)
B = len(b)
if A > B:
return self.addBinary(b, a)
ret = []
# a is shorter or equal
a_i = A - 1
b_i = B - 1
carry = False
while a_i >= 0:
if a[a_i] == '0' and b[b_i] == '0':
if carry:
ret.append('1')
else:
ret.append('0')
carry = False
elif a[a_i] == '0' and b[b_i] == '1' or a[a_i] == '1' and b[b_i] == '0':
if carry:
ret.append('0')
else:
ret.append('1')
carry = False
else:
if carry:
ret.append('1')
else:
ret.append('0')
carry = True
a_i -= 1
b_i -= 1
while b_i >= 0 and carry:
if b[b_i] == '0':
ret.append('1')
carry = False
else:
ret.append('0')
b_i -= 1
while b_i >= 0:
ret.append(b[b_i])
b_i -= 1
if carry:
ret.append('1')
ret.reverse()
return ''.join(ret)
# Runtime: 38 ms, faster than 86.47% of Python3 online submissions for Add Binary.
# Memory Usage: 14.1 MB, less than 24.29% of Python3 online submissions for Add Binary.