leetcode专题训练 67. Add Binary

这道题也很简单,我开始质疑我刷leetcode是不是不如再把acm捡起来。

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        count = 0
        l1 = len(a)
        l2 = len(b)
        l = min(l1, l2)
        c = ""
        i = l1-1
        j = l2-1
        while i >= 0 and j >= 0:
            anow = int(a[i])
            bnow = int(b[j])
            c = c+str(anow^bnow^count)
            count = (anow&bnow)|(anow&count)|(bnow&count)
            i -= 1
            j -= 1
        while i >= 0:
            anow = int(a[i])
            c = c+str(anow^count)
            count = anow&count
            i -= 1
        while j >= 0:
            bnow = int(b[j])
            c = c+str(bnow^count)
            count = bnow&count
            j -= 1
        if count:
            c = c+str(count)
        return c[::-1]

发布了219 篇原创文章 · 获赞 28 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/Ema1997/article/details/104136056