AC Python code

  • 0
    class Solution(object):
        def addBinary(self, a, b):
            :type a: str
            :type b: str
            :rtype: str
            n1, n2 = len(a), len(b)
            s = ''
            i, j, carry = n1-1, n2-1, 0
            while i>=0 or j>=0:
                d1 = int(a[i]) if i>=0 else 0
                d2 = int(b[j]) if j>=0 else 0
                d, carry = d1^d2^carry, carry&(d1+d2)|d1&d2
                s = str(d) + s
                i, j = i-1, j-1
            if carry!=0:    s = str(carry) + s
            return s

Log in to reply

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