Python simple one-liner: Using int(s, base)

  • 0

    I'd say this would be kind of cheating, but here's a one-liner:

    def addBinary(self, a, b):
        return '{:b}'.format(int(a,2) + int(b,2))

    The solution uses built-in int function's ability to convert from str to int type while specifying a base as a second argument i.e. 2 (binary) in this case. Then add the two resulting integers and format the sum as a binary string.

Log in to reply

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