Two python solutions


  • 1
    T

    1.one-line

    def addBinary(self, a, b):
        return bin(int(a, 2) + int(b, 2))[2:]
    

    2.solution based on https://leetcode.com/discuss/25593/short-code-by-c

    def addBinary(self, a, b):
        pa, pb, c, res = len(a) - 1, len(b) - 1, 0, ""
        while pa >= 0 or pb >=0 or c > 0:
            c += 1 if pa >= 0 and a[pa] == '1' else 0
            c += 1 if pb >= 0 and b[pb] == '1' else 0
            res = str(c % 2) + res
            pa, pb, c = pa - 1, pb - 1, c / 2
        return res

Log in to reply
 

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