Simple, easy to understand Python solution (no one-liner or built-in function)


  • 0
    R
    def addBinary(self, a, b):
            i = len(a)-1
            j = len(b)-1
            ret = ''
            c = 0
            
            while i >= 0 or j >= 0 or c == 1:
                sum = (i >= 0 and a[i] == '1') + (j >= 0 and b[j] == '1') + c
                ret = str(sum % 2) + ret
                c = sum / 2
                i -= 1
                j -= 1
                
            return ret
    

Log in to reply
 

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