Simple Python XOR solution beats 93%

  • 0

    The idea is simple:

    • XOR gates simulate binary addition without carryover
    • AND gates set carryover
    • LSHIFT moves the carryover

    Do this operation until there is no more carryover.

    def addBinary(self, a, b):
            a = int(a,2)
            b = int(b,2)
            while (b != 0):
                a, b = (a ^ b), (a & b) << 1
            return "{0:b}".format(a)

Log in to reply

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