美文网首页
python实现leetcode之67. 二进制求和

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

作者: 深圳都这么冷 | 来源:发表于2021-09-07 12:46 被阅读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)])
效果图

相关文章

网友评论

      本文标题:python实现leetcode之67. 二进制求和

      本文链接:https://www.haomeiwen.com/subject/hwejwltx.html