【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 ...
随机推荐
- 60款很酷的 jQuery 幻灯片演示和下载【转】
jQuery 是一个非常优秀的 JavaScript 框架,使用简单灵活,同时还有许多成熟的插件可供选择,它可以帮助你在项目中加入漂亮的效果,其中之一就是幻灯片效果的实现,这是一种在有限的网页空间内展 ...
- Linux系统中的文件权限
r(read) 4 可读 w(write) 2 可写 x() 1 可执行 644 755 rw-r-r ...
- [转] - Spark排错与优化
Spark排错与优化 http://blog.csdn.net/lsshlsw/article/details/49155087 一. 运维 1. Master挂掉,standby重启也失效 Mast ...
- angular.js学习笔记--概念总结
好久没更新了,现在开始学习学习angularjs,并且把学习到的一些知识总结记录一下,方便以后查找以及希望能给初学者一些帮助!(由于本人也是初学ng所以各位慎重理解!) 废话不多说,开始! $root ...
- ThinkPHP框架 3.2.2 获取系统常量信息 连接数据库 命名空间的理解
获取系统常量信息 随便一个方法里加上这句话都能获取到系统常量信息!! var_dump(get_defined_constants()); <?php namespace Admin\Contr ...
- db2命令参数with ur
查询DB2数据库,老遇到select * from XXX with ur, 好奇ur是什么作用(转) DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级别来隔离数据. ...
- 程序报错java.lang.OutOfMemoryError: PermGen space
参考文档: http://www.cnblogs.com/xwdreamer/archive/2011/11/21/2296930.html http://www.cnblogs.com/ceshi2 ...
- 洛谷P1032 字串变换【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1032 题意: 给定一个原字符串和目标字符串,以及几个字符串变换的规则. 问能否根据这几个规则在十步之内把原字 ...
- 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...
- CMake error with move_base_msgs问题解决
错误 CMake Error at /opt/ros/groovy/share/catkin/cmake/catkinConfig.cmake: (find_package): Could not f ...