首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

数据结构算法操作试题(C++/Python)——字符串相乘

1. 题目

leetcode 链接:https://leetcode-cn.com/problems/multiply-strings/

2. 解答

  1. 进位相乘 python: 564ms, 11.8mb
代码语言:javascript
复制
class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        res = 0
        for i in range(1,len(num1)+1):
            for j in range(1, len(num2)+1):
                res += int(num1[-i]) * int(num2[-j]) *10**(i+j-2)
        return str(res)
  1. 转整型 python 24ms, 11.8MB
代码语言:javascript
复制
class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
    return str(int(num1) * int(num2))

其他方法看 leetcode 链接 评论区~

下一篇
举报
领券