43. leetcode题目讲解(Python):字符串相乘(M
2018-12-14 本文已影响63人
夏山闻汐
题目如下:

思路:
这道题值得注意的是,要求不能直接将string 转换为 int。
参考代码:
class Solution:
def str2num(self, num):
int_num = 0
pos = 1
for n in num[::-1]:
int_num += (ord(n) - 48) * pos
pos = pos * 10
print(int_num)
return int_num
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
n1 = self.str2num(num1)
n2 = self.str2num(num2)
return repr(n1 * n2)
源码地址:
https://github.com/jediL/LeetCodeByPython
其它题目:[leetcode题目答案讲解汇总(Python版 持续更新)]
(https://www.jianshu.com/p/60b5241ca28e)
ps:如果您有好的建议,欢迎交流 :-D,
也欢迎访问我的个人博客 苔原带 (www.tundrazone.com)