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. url自动补全index.php

    location / { index index.html index.htm index.php l.php; autoindex on; if (!-e $request_filename) { ...

  2. 设计模式学习--Builder

    What Builder:将一个复杂的对象的构建和表示分离,使得同样的构建过程可以创建不同的表示. Why Builder也是创建型模式的一种,它是一步一步的向导式的创建一个复杂的对象,Builder ...

  3. ABP之事件总线(1)

    什么是事件总线呢?官方的文档说,它是一个单例对象,由其他的类共同拥有,可以用来触发和处理事件.这个东西确实比较陌生,为什么要使用事件总线,或者说事件总线的优势是什么???首先我们可以明确的是,事件总线 ...

  4. db2 v9.7 新特性cur_commit 能够实现未提交读新特性cur_commit 能够实现未提交读

    db2 get db cfg|find "CUR_COMMIT" 当前已落实                                   (CUR_COMMIT) = ON ...

  5. easyui datagrid 弹出页面会出现两个上下滚动条处理办法!

    同事推荐将datagrid上加一个toolbar 将上面的工具元素加上就可以了 toolbar: '#divListToolbar',

  6. linux:nano 、cat和file

    nano 在 Linux 下面编辑文件通常我们会直接使用专门的命令行编辑器比如(emacs,vim,nano),涉及 Linux 上的编辑器的内容比较多,且非常重要. nano 是 linux 的一款 ...

  7. 创建ReactNative的iOS项目

    http://reactnative.cn/docs/integration-with-existing-apps/ 1.安装好ReactNative开发环境 2.安装好CocoaPods 3.创建项 ...

  8. express工程的优化和请求参数的处理

    1.让工程自动刷新 在Express的默认工程中,ejs, jade等模板的改变会立刻被渲染到浏览器中,但是js的改变不能立即刷新.这时候我们要用到一些自动刷新工具, 如 nodemon, super ...

  9. arcengine右键实现new group layer的功能

    没有找到相关方法,但是有对图层组进行操作的资料. https://gis.stackexchange.com/questions/43620/how-do-i-reach-a-layer-inside ...

  10. nslookup dig iptables,sudoer,jenkins

    [NSLOOKUPm]http://roclinux.cn/?p=2441 nslookup media.ucampus.unipus.cn [DIG]http://roclinux.cn/?p=24 ...