题目链接
【题目】
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
The numbers can be arbitrarily large and are non-negative.
Converting the input string to integer is NOT allowed.
You should NOT use internal library such as BigInteger.
【分析】
先看下python的写法:
使用reduce是及其简便的写法,而且效率很高
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
num1 = reduce(lambda x, y: x * 10 + y, [ord(i) - 48 for i in num1]) # '0' is 48 in ascii
num2 = reduce(lambda x, y: x * 10 + y, [ord(i) - 48 for i in num2])
return str(num1 * num2)
打败了95%
这里的[ord(i) - 48 for i in num]可以改写成map形式,会快那么一点点
class