【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/multiply-strings/description/
题目描述
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 and num2 is < 110.
- Both num1 and num2 contain only digits 0-9.
- Both num1 and num2 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.
题目大意
实现字符串表示的数字乘法。
解题方法
先抖个机灵:
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
return str(int(num1) * int(num2))
上面的能过,下面正经点。
此题是让我们模拟乘法,所以计算方法也就是模拟了小学数学的列竖式。从末尾数字开始计算乘积,注意进位,先得到num2中每个数字与num1的乘积,再通过10的多少次方的形式代表其位数。啊,描述起来太难了,可以想想竖式怎么列的。
另外,这个题用python这么做是不合理的,因为Python的int可以无限大的,所以没有真正实现了大数乘法。
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
if num1 == '0' or num2 == '0':
return '0'
ans = 0
for i, n1 in enumerate(num2[::-1]):
pre = 0
curr = 0
for j, n2 in enumerate(num1[::-1]):
multi = (ord(n1) - ord('0')) * (ord(n2) - ord('0'))
first, second = multi // 10, multi % 10
curr += (second + pre) * (10 ** j)
pre = first
curr += pre * (10 ** len(num1))
ans += curr * (10 ** i)
return str(ans)
根据JustDoIT的做法,首先我们把每一位相乘,得到一个没有进位的临时结果,如图中中间的一行红色数字就是临时结果,然后把临时结果从低位起依次进位。对于一个m位整数乘以n位整数的结果,最多只有m+n位。

C++代码如下:
class Solution {
public:
string multiply(string num1, string num2) {
const int M = num1.size();
const int N = num2.size();
const int k = M + N - 2;
vector<int> res(M + N, 0);
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
res[k - i - j] += (num1[i] - '0') * (num2[j] - '0');
}
}
int carry = 0;
for (int i = 0; i < res.size(); ++i) {
res[i] += carry;
carry = res[i] / 10;
res[i] %= 10;
}
int start = res.size() - 1;
while (start >= 0 && res[start] == 0)
--start;
if (start < 0) return "0";
string ans;
while (start >= 0) {
ans += char(res[start] + '0');
--start;
}
return ans;
}
};
日期
2018 年 6 月 13 日 —— 腾讯赛圆满结束!两个月修得正果哈哈~~
2019 年 3 月 3 日 —— 3月开始,春天到了
【LeetCode】43. Multiply Strings 解题报告(Python & C++)的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- leetcode 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 ...
随机推荐
- Python中的随机采样和概率分布(二)
在上一篇博文<Python中的随机采样和概率分布(一)>(链接:https://www.cnblogs.com/orion-orion/p/15647408.html)中,我们介绍了Pyt ...
- Android editttext只能输入不能删除(选中后被软键盘遮住)
感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.
- 学习java的第十天
一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...
- 如何使用 Kind 快速创建 K8s 集群?
作者|段超 来源|尔达 Erda 公众号 导读:Erda 作为一站式云原生 PaaS 平台,现已面向广大开发者完成 70w+ 核心代码全部开源!在 Erda 开源的同时,我们计划编写<基于 ...
- 【Reverse】每日必逆0x00
附件:https://files.buuoj.cn/files/aa4f6c7e8d5171d520b95420ee570e79/a9d22a0e-928d-4bb4-8525-e38c9481469 ...
- 最长公共子序列问题(LCS) 洛谷 P1439
题目:P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 关于LCS问题,可以通过离散化转换为LIS问题,于是就可以使用STL二分的方法O(nlogn ...
- vmware使用nat连接配置
一.首先查看自己的虚拟机服务有没有开启,选择电脑里面的服务查看: 1.计算机点击右键选择管理 2.进入管理选择VM开头的服务如果没有开启的话就右键开启 二.虚拟机服务开启后就查看本地网络虚拟机的网 ...
- GO 通过进程号输出运行运行信息
操作系统应用可以使用PID来查找关于进程本身的信息.当进程失败时获取到的PID就非常有价值,这样就可以使用PID跟踪整个系统中的系统日志,如/var/log/messages./var/log/sys ...
- VectorCAST软件自动化测试方案
VectorCAST 是主要用于对C/C++/Ada程序进行软件自动化测试,并能够在Windows和Linux等多种开发环境下运行.其主要功能包含自动化的单元测试.集 成测试.覆盖率分析.回归测试.代 ...
- antd动态的表格合并(包含排序功能)
主要是两个步骤, 1.处理接口返回数据,给其添加两个属性,一个是合并行数(列数),一个是当前数据的序号 2.在columns结合antd官网的处理方法合并表格 3.尽可能得减少计算量 数据处理函数 / ...