67.二进制求和

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        result, carry, val = '', 0, 0
        for i in range(max(len(a), len(b))):
            val = carry
            if i < len(a):
                val += int(a[-(i+1)])
            if i < len(b):
                val += int(b[-(i+1)])
            carry, val = val//2, val%2
            result += str(val)
        if carry:
            result += str(1)
        return result[::-1]

 

上一篇:add-binary(二进制相加)


下一篇:PAT A1024 Palindromic Number (25 分) 大数加法