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

文章目录


数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

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

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

2. 解答

  1. 进位相乘 python: 564ms, 11.8mb

    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)
    
  2. 转整型 python 24ms, 11.8MB

    class Solution(object):
        def multiply(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
        return str(int(num1) * int(num2))
    

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