Python concise solution with two-pointer.


  • 0
    C
    def addBinary(self, a, b):
        i, j, carry, res = len(a)-1, len(b)-1, 0, ""
        while i >= 0 or j >= 0 or carry:
            if i >= 0:
                carry += int(a[i])
                i -= 1
            if j >= 0:
                carry += int(b[j])
                j -= 1
            res = str(carry%2) + res
            carry //= 2
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.