【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 ...
随机推荐
- xmake v2.6.1 发布,使用 Lua5.4 运行时,Rust 和 C++ 混合编译支持
xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...
- SpringBoot 整合 MyBatis,实现 CRUD 示例
目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...
- Centos7部署RabbitMQ的镜像队列集群
一.背景 在上一章节中,我们学会了如何搭建一个单节点的RabbitMQ服务器,但是单节点的RabbitMQ不可靠,如果单节点挂掉,则会导致消息队列不可用.此处我们搭建一个3个节点的RabbitMQ集群 ...
- Maven 目录结构[转载]
转载至:http://www.cnblogs.com/haippy/archive/2012/07/05/2577233.html Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目, ...
- C++构造函数和析构函数初步认识(2)
构造函数的三个作用1.构造对象2.对象初始化3.类型转换 //Test1.h #include<iostream> using namespace std; //构造对象 //初始化对象 ...
- IDEA2021.2安装与配置
https://blog.csdn.net/qq_37242720/article/details/119349394
- git删除了本地文件,从远程仓库中恢复
在本地删除了文件,使用git pull,无法从远程项目中拉取下来 具体操作 查看项目的状态,会显示出你删除的数据 git status 进入被删除的文件的目录下,假设删除的文件名为 test.txt ...
- vue-cli4脚手架搭建三
组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...
- 阿里云esc 安装 mysql5.7.27
1. 下载: wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...
- 【Java 设计】如何优雅避免空指针调用
空指针引入 为了避免空指针调用,我们经常会看到这样的语句 if (someobject != null) { someobject.doCalc();} 最终,项目中会存在大量判空代码,多么丑陋繁冗! ...