Skip to content

Commit

Permalink
手写二进制加法
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Sep 30, 2018
1 parent 22a9f3d commit 8692c85
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lc67. Add Binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
res = ''
mask = 0
for i in range(1, 2 + max(len(a), len(b))):
if len(a) >= i:
tem_a = int(a[-i])
else:
tem_a = 0
if len(b) >= i:
tem_b = int(b[-i])
else:
tem_b = 0
num = mask + tem_a + tem_b
q = num // 2
r = num % 2
res = str(r) + res
mask = q
if res[0] == '0':
res = res[1:]
return res


a = "1010"
b = "1011"
s = Solution()
print(s.addBinary(a, b))

# 也可以直接调用内置函数
# return str(bin(int(a, 2) + int(b, 2)))[2:]
# int(a,2) 以2进制表示的str,转为int
# bin(n) 转为二进制 如:'0b1011'
# 所以最后才去掉前两位

0 comments on commit 8692c85

Please sign in to comment.