-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddbinary.py
More file actions
28 lines (28 loc) · 841 Bytes
/
addbinary.py
File metadata and controls
28 lines (28 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
26
27
28
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
len_a = len(a)
len_b = len(b)
if len_a < len_b:
a = (len_b - len_a)*'0' + a
else:
b = (len_a - len_b)*'0' + b
c = 0
result = ''
for i in range(len(a))[::-1]:
if ord(a[i]) - ord('0') + ord(b[i]) - ord('0') + c == 3:
result = '1' + result
c = 1
elif ord(a[i]) - ord('0') + ord(b[i]) - ord('0') + c == 2:
result = '0' + result
c = 1
else:
result = str(ord(a[i]) - ord('0') + ord(b[i]) - ord('0') + c) + result
c = 0
if c == 1:
result = '1' + result
return result