【LeetCode每天一题】Multiply Strings(字符串乘法)
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:
- The length of both
num1andnum2is < 110. - Both
num1andnum2contain only digits0-9. - Both
num1andnum2do not contain any leading zero, except the number 0 itself. - 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(字符串乘法)的更多相关文章
- LeetCode OJ:Multiply Strings (字符串乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode 第42题 Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- Multiply Strings(字符串乘法模拟,包含了加法模拟)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
随机推荐
- linux 下开源代理路由工具
服务器搭建,参考 https://gfw.press/blog/?p=21 运行环境 openjdk1.8,linux 1.首先,获取工具地址 git clone https://github.com ...
- 如何将MP3录音转文字
相信很多人都有电话录音的习惯,因为这样可以记录下很多重要的信息.那么当我们通过录音将一些重要的信息记录下来后,我们应该怎样将这些录音文件转换成文字进行记录呢?下面我们就一起来看一下吧. 操作步骤: 步 ...
- RasterEdge.DocImageSDK9.8.7 破解版
RasterEdge.DocImageSDK9.8.7 破解版 本人破解了 RasterEdge.DocImageSDK9.8.7 ,有需要的同学请联系本人.
- 洛谷 P1059明明的随机数 & P1068分数线划定 & P1781宇宙总统
题目:https://www.luogu.org/problemnew/show/P1059 思路:STL中的set使用. //#include<bits/stdc++.h> #inclu ...
- 国庆JAVA作业
动手动脑1 运行enumtest.java程序我明白了JAVA中枚举类型,s和t不能引用同一个对象.不是原始对象,可以实现从字符串中的转换. 动手动脑2 原码就是符号位加上真值的绝对值, 即用第一位表 ...
- 远程登录oracle 12.2数据库报错ORA-28040解决办法
今天新安装的oracle 12.2.0.1数据库,通过本地sqlplus远程登录12c数据库报错ora-28040,如下: ORA-28040: No matching authentication ...
- PHP之Boolean
Boolean布尔类型 这是最简单的类型.Boolean表达了真值,可以为true或者false. 语法 要指定一个布尔值,使用常量true或者false,两个都不区分大小写. 通常运算符所返回的Bo ...
- dispatch_barrier_async,加锁
dispatch_barrier_async用于文件读写,在写的时候,不允许进行读操作,在写操作完成才能进行读取操作,为了保证线程安全 加锁的方式: 1. barrier 2. NSLock 3. @ ...
- 什么是restful api
https://blog.csdn.net/laotianv5/article/details/81634997 什么是Restful API Restful API 从字面就可以知道,他是rest式 ...
- shiro入门实例,基于ini配置
基于ini或者关系数据库的,其实都是一样的,重要的是思想. # ==================================================================== ...