Python Easy and Elegant Solution


  • 0
    D
    class Solution(object):
        def multiply(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            n1, n2 = len(num1), len(num2)
            
            def mul(i, j, l1, l2):
                if l1 > 1: return sum(mul(i+k-1, j, 1, l2)*10**(l1-k) for k in range(1, l1+1))
                if l2>1: return sum(mul(i, j+k-1, l1, 1)*10**(l2-k) for k in range(1, l2+1))
                return int(num1[i])*(int(num2[j]))
                
            return str(mul(0, 0, n1, n2))

Log in to reply
 

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