Share my python solution


  • 0
    F
    class Solution(object):
        def addBinary(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: str
            """
            if len(a)<len(b):#make a is the longer binary bits
                a, b=b, a
            b='0'*(len(a)-len(b))+b
            carry=0
            rst=""
            for i in range(len(a)-1, -1, -1):
                temp=int(a[i])+int(b[i])+carry
                rst=str(temp%2)+rst
                carry=temp/2
            if carry==1:
                rst='1'+rst
            return rst

Log in to reply
 

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