Simple python solution with list


  • 0
    G
    enter code hereclass Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        
        
        if len(a)<=len(b):
            short=a
            long=b
        else:
            short=b
            long=a
        
        longl=[]
        shortl=[]
        for i in range(0,len(short)):
            shortl.append(int(short[i]))
        for i in range(0,len(long)):
            longl.append(int(long[i]))
    
        for i in range(0,len(long)-len(short)):
            shortl.insert(0,0)
            
        for i in range(0,len(long)):
            longl[i]=shortl[i]+longl[i]
        
        for i in range(len(longl)-1,0,-1):
            if longl[i]>=2:
                longl[i]-=2
                longl[i-1]+=1
        if longl[0]>=2:
            longl[0]-=2
            longl.insert(0,1)
        
        res=''
        for i in longl:
            res+=str(i)
        return res

Log in to reply
 

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