解题思路
从尾部往前逐位加,注意进位
67. 二进制求和
代码
class Solution(object):
from itertools import zip_longest
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
rtv = []
c = 0
for x, y in zip_longest(a[::-1], b[::-1], fillvalue=0):
c, v = divmod(int(x) + int(y) + c, 2)
rtv.append(v)
if c:
rtv.append(c)
return ''.join([str(i) for i in reversed(rtv)])

网友评论