leetcode-二进制求和

67. 二进制求和

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

你可能感兴趣的:(leetcode)