Python using map reduce, beats 89%


  • 1
    K
    from functools import reduce
    class Solution(object):
        def convert(self,i):
            m = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
            return m[i]
        def multiply(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            if num1 == '0' or num2 == '0': return '0'
            if num1 == '1': return num2
            if num2 == '1': return num1
            i = reduce(lambda x,y : 10 * x + y,map(self.convert,list(num1)))
            j = reduce(lambda x,y : 10 * x + y,map(self.convert,list(num2)))
            return str(i * j)

  • 0
    2

    Not valid because you converted inputs into numbers.


Log in to reply
 

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