-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67.py
39 lines (28 loc) · 795 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
33
34
35
36
37
38
39
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
n = len(a)
m = len(b)
max_l = max(n, m)
sum_l = ["0"] * max_l
i = 1
plus = 0
while i <= max_l:
n1 = int(a[n-i]) if n >=i else 0
n2 = int(b[m-i]) if m >= i else 0
sum_l[max_l-i] = str((n1 + n2 + plus) % 2)
plus = (n1 + n2 + plus) / 2
i += 1
if plus > 0:
return str(plus) + "".join(sum_l)
else:
return "".join(sum_l)
if __name__ == "__main__":
print Solution().addBinary("111", "101")