-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Binary.py
More file actions
40 lines (37 loc) · 959 Bytes
/
Copy pathAdd Binary.py
File metadata and controls
40 lines (37 loc) · 959 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
29
30
31
32
33
34
35
36
37
38
39
40
"""
@Project: leetcode
@file: Add Binary.py
@author: AC
@time: 2016/5/11
@Description: Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
"""
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
max_len = max(len(a), len(b))
aa = ['0']*(max_len-len(a))+list(a)
bb = ['0']*(max_len-len(b))+list(b)
jinwei = 0
result = []
for i in xrange(max_len-1, -1, -1):
temp = int(aa[i])+int(bb[i])+jinwei
if temp >= 2:
result.append(str(temp-2))
jinwei = 1
else:
result.append(str(temp))
jinwei = 0
if jinwei == 1:
result.append('1')
result.reverse()
return ''.join(result)
s = Solution()
print s.addBinary('1', '1')