-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67.py
32 lines (29 loc) · 781 Bytes
/
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
class Solution:
# @param {string} a
# @param {string} b
# @return {string}
def addBinary(self, a, b):
len_a = len(a)
len_b = len(b)
max_len = max(len_a,len_b)
finalstr = ["0"] * max_len
a = "0" * (max_len - len_a) + a
b= "0" * (max_len - len_b) + b
extra = 0
for i in range(max_len-1,-1,-1):
ai = int(a[i])
bi = int(b[i])
sumall = ai+bi+extra
finalstr[i] = str(sumall % 2)
if sumall >= 2:
extra = 1
else:
extra = 0
if extra == 1:
finalstr.insert(0,"1")
finalstr = ''.join(finalstr)
return finalstr
a = "101"
b = "1"
sol = Solution()
print sol.addBinary(a,b)