【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
num1
andnum2
is < 110. - Both
num1
andnum2
contain only digits0-9
. - Both
num1
andnum2
do 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 ...
随机推荐
- Java学习者论坛【申明:来源于网络】
学习者论坛[申明:来源于网络] 1.Java学习者论坛 2.51论坛 3.csdn论坛 4.JAVA ME论坛 地址|: http://www.javaxxz.com/ 地址|: http://bbs ...
- $(").each 和$.each
$(").each 这个是遍历dom树的,遍历数组的会报not afunction
- easyui---combogrid
选择一个数据表格某行记录 依赖panel 和datagrid combogrid:<select id="cc" name="dept" style=&q ...
- js积累点
window.opener.parent.frames['taskAnswerInfoForm'].location=newUrl;//可以使该frame的页面跳转.不需要再写xxx.location ...
- svn异常:subversion.javahl.ClientException
使用svn时出现异常: INFO [org.netbeans.modules.subversion]: org.apache.subversion.javahl.ClientException: Pr ...
- C# 调用存储过程 Sql Server存储过程 存储过程报错,程序中的try
C#程序调用Sql Server存储过程,存储过程中报错情况,返回值... 0.SQL存储过程 USE [Opos] GO /****** Object: StoredProcedure [dbo]. ...
- 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)
We define the smallest positive real number as the number which is explicitly greater than zero and ...
- 最全最详细:ubuntu16.04下linux内核编译以及设备驱动程序的编写(针对新手而写)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- [development][C] C语言标准
GUN C的标准文档: 也就是glibc https://www.gnu.org/software/libc/ http://man7.org/linux/man-pages/dir_section_ ...
- [potatos][flex][TBC] 语义分析词法分析 flex
FLEX: The Fast Lexical Analyzer https://github.com/westes/flex 这并不是我的人生中第一次遇见flex,好多工程中,我都发现他们用到了fle ...