43. Multiply Strings 字符串相乘
1. 原始题目
给定两个以字符串形式表示的非负整数 num1
和 num2
,返回 num1
和 num2
的乘积,它们的乘积也表示为字符串形式。
示例 1:
输入: num1 = "2", num2 = "3"
输出: "6"
示例 2:
输入: num1 = "123", num2 = "456"
输出: "56088"
说明:
num1
和num2
的长度小于110。num1
和num2
只包含数字0-9
。num1
和num2
均不以零开头,除非是数字 0 本身。- 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。
2. 思路
对于两个长为num1和num2的整数相乘,结果最长为num1+num2,所以先初始化这样长的一个数组。然后可以看到建立两个循环来存放每个索引的结果,当前的两个数字相乘后会在数组的两个位置产生结果。即商和余数。依次便利即可。存放完成后再对数组进行一边处理,因为有的位置可能>9,需要再进位至前一位。如果开头是0的话要删除。如果全0数组只返回”0“即可。
3. 实现
class Solution:
def multiply(self, num1: str, num2: str) -> str:
res = [0]*(len(num1)+len(num2))
for i in range(len(num1)-1, -1, -1): # 2,1,0
for j in range(len(num2)-1, -1, -1): # 1,0
temp = int(num1[i])*int(num2[j])
res[i+j+1] += temp%10 # 更新第一个索引位置
res[i+j] += temp // 10 # 更新第二个索引位置
for i in range(len(res)-1,-1,-1): # 将数组在遍历一遍更新>9的数,进位
if res[i]>9:
res[i-1]+=res[i]//10
res[i] = res[i]%10
res = ''.join([str(s) for s in res]).lstrip('') # 删掉开头的0
if not res: return '' # 如果全0的话只返回"0"就好
return res
43. Multiply Strings 字符串相乘的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
- 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 ...
- 43. Multiply Strings 字符串表示的大数乘法
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- 043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意: num1 和 num2 的长度均小于110. num1 和 num2 均只包含数字 0 ...
- Leetcode43. Multiply Strings字符串相乘(大数相乘)
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
随机推荐
- 弹出table页面--hq
function queryRelation(tableID,prosourceID){ //弹出页面 debugger; initqueryRelationGrid(tableID,prosour ...
- 阅读redis源代码的一些体会
最近在学习redis及阅读redis等程序的源码时,有一些收获,特记录到下面. 1.第一步,阅读源代码借助最好可以跟踪的工具去读,如sourceinsight. 我使用的是windows7环境,又因为 ...
- Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException
Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException 描述 在用eclipse进行debug的时候弹出了上面的错误,在s ...
- 利用pandas生成csv文件
# -*- coding:UTF-8 -*- import json from collections import OrderedDict with open('dns_status.json',' ...
- rsync的配置
1. 安装rsync,如果要讲1.1.1.1的数据备份到2.2.2.2上,则在1.1.1.1上配置如下: /etc/rsyncd.conf uid = nobody gid = nobody use ...
- 【linux相识相知】独立硬盘冗余阵列-RAID
独立硬盘冗余阵列(RAID,Redundant Array of Independant Disks),旧称为廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks ...
- [转].NET Core之Entity Framework Core 你如何创建 DbContext
本文转自:http://www.cnblogs.com/tdws/p/5874212.html 本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www. ...
- union、except和intersect查询
1. union联合查询 (合并) select r.room_id from room r union select rp.num from room_type rp 要求表1和表2的查询结果结构 ...
- FreeSwitch无法启动,提示进程pid锁定的解决方法
来源:https://stackoverflow.com/questions/12817232/how-do-i-call-a-local-softphone-on-freeswitch error ...
- [转]最全Redis面试题整理
此为转载文章,仅做记录使用,方便日后查看,原文链接:http://www.bieryun.com/3405.html 1.什么是Redis? 答:Redis全称为:Remote Dictionary ...