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

时间:2022-07-24
本文章向大家介绍数据结构算法操作试题(C++/Python)——字符串相乘,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 题目

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

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)
  1. 转整型 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 链接 评论区~