python O(n)


  • 0
    S
    class Solution(object):
        def addBinary(self, a, b):
            lista = list(a)
            listb = list(b)
            res = []
            carry = 0
            while lista or listb or carry != 0:
                if lista:
                    a = int(lista.pop())
                else:
                    a = 0
                if listb:
                    b = int(listb.pop())
                else:
                    b = 0
                temp = a + b + carry
                res.append(str(temp%2))
                carry = temp/2
            res.reverse()    
            return "".join(res)
    

Log in to reply
 

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