My Python Solution


  • 0
    S
        def multiply(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            #l1=[int(i) for i in num1]
            #l2=[int(i) for i in num2]
            val=0
            carry=0
            sum=0
            flag=0
            product=0
            res=[]
            k=0
            i=len(num1)-1
            j=len(num2)-1
            flag=0
            if len(num1)>len(num2):
               i,j=j,i
               flag=1
    
            while i>=0:
                    res=[]
                    while j>=0:
                        if flag==1:
                           val=((ord(num2[i])-ord('0'))*(ord(num1[j])-ord('0'))+carry)
                        else:   
                           val= ((ord(num1[i])-ord('0'))*(ord(num2[j])-ord('0'))+carry)  
                        res=[str(val%10)]+res
                        #print res
                        carry=val/10
                        j-=1
                    if carry!=0:    
       res=[str(carry)]+res    
                    product=int(''.join(res)) 
                    carry=0
                   # print product
                    sum=sum+((product)*(10**k))
                    #print sum
                    k+=1
                    i-=1
                    if flag==1:
                       j=len(num1)-1
                    else:   
                       j=len(num2)-1   
            return str(sum)

  • 0

    Converting the input string to integer is NOT allowed.
    haven't you see it?


  • 0
    S

    @sharing-account I thought we cannot perform direct int operations
    like return int(s1)*int(s2).However thanks for pointing out I have now edited my code.
    If there are still any concerns feel free to point out


Log in to reply
 

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