Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"                    Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"             Output: "56088"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly

思路


  这道题和之前做的字符串加法挺相似的,唯一有区别的是,加法和乘法的计算规则不一样。加法是对应位置相加,只要一次遍历。而乘法则是需要两层循环,因为num1中的每一位都需要乘以num2,最后相加得到结果。这就意味着在一定程度上乘法的细节处理更加繁琐一些。但是只要抓住核心部分最终都能写出来。时间复杂度为O(n*m)(n,m分别为num1和num2的长度),空间复杂度为O(m+n)。

实现代码


 class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res_list = [0] * (len(num1) + len(num2)) # 存储最终结果的数组
start = len(res_list)-1 # 每一次循环相加位置的下标 for n1 in reversed(num1): # 两层循环,意思是使用num2中每一位乘以num1中的每一位
tempPos = start # 当前循环内的下标,意思是num1个位乘以num2的每一位时,第一位存储的是个位。
for n2 in reversed(num2):
res_list[tempPos] += int(n1) * int(n2) # 存储当前结果
res_list[tempPos-1] += res_list[tempPos]/10 #存储低一位溢出的数
res_list[tempPos] %= 10 # 存储个位的数
tempPos -= 1 # 进一位,意思是下一次结果存储的位置
start -= 1 # i = 0
while i < len(res_list)-1 and res_list[i] == 0: # 去除前面的0, 并且防止最终结果为零的输出。
i += 1 return ''.join(map(str, res_list[i:])) # 返回字符串

【LeetCode每天一题】Multiply Strings(字符串乘法)的更多相关文章

  1. LeetCode OJ:Multiply Strings (字符串乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. leetcode 第42题 Multiply Strings

    题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...

  3. Multiply Strings(字符串乘法模拟,包含了加法模拟)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  5. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  6. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  8. [Leetcode] Multiply strings 字符串对应数字相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. [leetcode]43. Multiply Strings高精度乘法

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

随机推荐

  1. js 使用a标签 下载资源

    文档 let data = new Blob(['hello ajanuw'], { type: 'application/text' }) let src = window.URL.createOb ...

  2. li下的ul----多级列表

    <ul id="ul_Style1"> <li>第1级第1行</li> <li> <ul id="ul_Style2 ...

  3. Inotify+rsync实现实时数据同步

    使用rsync可以实现数据同步,但是即使使用crontab定时任务最小执行间隔为1分钟,在数据实时性要求比较高场合需使用inotify+rsync实现实时同步 下载inotify wget https ...

  4. ROS rosrun 调用 sudo 命令

    https://blog.csdn.net/qq_16775293/article/details/81138904

  5. apache中端口与目录的关系

    apache中,如果设置了监听了端口而没有配置对应的虚拟目录,则访问时会指向<Directory >节所指向的目录.

  6. XDCTF网络安全大赛——Web100

    打开网址 http://game1.xdctf.com:8083/f16c3b1ed800fc78e605/index.php 网页中显示了一张图片,图片名为hackkey.png: 还有一段文字: ...

  7. C-Free 5 安装 [Error] G__~1.EXE: (x86)\C-FREE~1\mingw\mingw32\bin\: No such file or directory

    解决[Error] g++.exe: 5\mingw\include: No such file or directory - 陆总的博客 - CSDN博客 https://blog.csdn.net ...

  8. iOS - 常用的CG结构体

    CGPoint.CGSize.CGRect.CGRectEdge实际上都是结构体 一,几个常用的结构体 CGPoint   定义一个点,设置x坐标和y坐标 struct CGPoint { CGFlo ...

  9. LeetCode 892 Surface Area of 3D Shapes 解题报告

    题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of ...

  10. Maven基本介绍及安装

    什么是Maven 是一个跨平台的项目管理工具. 跨平台是指它几乎可以在现有所有流行的操作系统中运行 maven不仅可以构建项目,还可以依赖管理和项目信息管理 Maven解决了什么问题 maven解决了 ...