python实现leetcode之67. 二进制求和

2021-09-07  本文已影响0人  深圳都这么冷

解题思路

从尾部往前逐位加,注意进位

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)])
效果图
上一篇 下一篇

猜你喜欢

热点阅读