Python, 55ms, with 2 for and 3 while


  • 0
    Z
    class Solution(object):
        def addBinary(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: str
            """
            a, b, c, rs = [int(s) for s in a], [int(s) for s in b], 0, ''
            while a and b:
                va, vb = a.pop(), b.pop()
                rs = str(va ^ vb ^ c) + rs
                c = 1 if va + vb + c > 1 else 0
            while a:
                va = a.pop()
                rs = str(va ^ c) + rs
                c = 1 if va + c > 1 else 0
            while b:
                vb = b.pop()
                rs = str(vb ^ c) + rs
                c = 1 if vb + c > 1 else 0
            if c:
                rs = str(c) + rs
            return rs
            
            
    

Log in to reply
 

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