Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
multiply-strings.py 917 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 13:04 +08:00 . update
# Time: O(m * n)
# Space: O(m + n)
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
num1, num2 = num1[::-1], num2[::-1]
res = [0] * (len(num1) + len(num2))
for i in xrange(len(num1)):
for j in xrange(len(num2)):
res[i + j] += int(num1[i]) * int(num2[j])
res[i + j + 1] += res[i + j] / 10
res[i + j] %= 10
# Skip leading 0s.
i = len(res) - 1
while i > 0 and res[i] == 0:
i -= 1
return ''.join(map(str, res[i::-1]))
# Time: O(m * n)
# Space: O(m + n)
# Using built-in bignum solution.
class Solution2(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
return str(int(num1) * int(num2))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助