forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2.py
More file actions
25 lines (25 loc) · 767 Bytes
/
solution2.py
File metadata and controls
25 lines (25 loc) · 767 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
class Solution:
# @param a, a string
# @param b, a string
# @return a string
def addBinary(self, a, b):
list_a = [int(i) for i in a[::-1]]
list_b = [int(i) for i in b[::-1]]
la = len(list_a)
lb = len(list_b)
# Pad zeroes
if la < lb:
list_a += [0 for i in range(lb - la)]
la = len(list_a)
else:
list_b += [0 for i in range(la - lb)]
lb = len(list_b)
carry = 0
res = []
for i in range(la):
t = (list_a[i] + list_b[i] + carry) % 2
carry = (list_a[i] + list_b[i] + carry) / 2
res.append(t)
if carry == 1:
res.append(1)
return ''.join([str(d) for d in res[::-1]])