-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.py
More file actions
25 lines (22 loc) · 841 Bytes
/
addBinary.py
File metadata and controls
25 lines (22 loc) · 841 Bytes
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
# problem: https://leetcode.com/problems/add-binary/
# Runtime: 59 ms, faster than 9.18% of Python3 online submissions for Add Binary.
# Memory Usage: 14.2 MB, less than 55.41% of Python3 online submissions for Add Binary.
class Solution:
def addBinary(self, a: str, b: str) -> str:
l = []
n = max(len(a), len(b))
a = list(reversed(a))
b = list(reversed(b))
carry = "0"
for i in range(n + 1):
x = a[i] if i < len(a) else "0"
y = b[i] if i < len(b) else "0"
if i == n and carry == x == y == "0":
break
if x == y:
l.append(carry)
carry = x
else:
val = "0" if carry == "1" else "1"
l.append(val)
return "".join(reversed(l))