Multiply Strings——面试题
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
这道题就是模拟乘法思维了,还需要模拟加法思维,每一位乘以一个数都要和前面的结果加起来。
注意:
1 要把这两个操作过程分清楚,不能混饶了,否则会结果不正确的。
2 乘法有进位,和前面的结果加起来也有加法进位,一定要分清楚。
3 每一次一个新数位与被乘数相乘之前,都一定要把两个进位加在结果上。
4 同时需要把两个进位值都清零。
整体思路:
这道题的要求是计算大数乘法。其中大数是以字符串的形式表示,任意大,非负,返回结果以字符串形式。
这道题其实就是模拟整数乘法。
假设两个整数的长度分别为了l1和l2,则其最后结果长度为l1+l2(最后有进位)或者l1+l2-1(最后没有有进位)。
因此,可以先用长度为l1+l2的数组记录结果,最后再转成字符串。
进行乘法的时候,先把各个位的相乘结果对应累加起来,即第1个整数的第i位(低位到高位)和第2个整数的第j位(低位到高位)相乘的结果应该存放在数组的i+j位。然后再统一处理进位。
然后再统一处理进位。
最后再将数组转成字符串前,需要跳过前面的零。如果结果只有0,则只返回0。
时间复杂度:O(l1l2)(l1和l2分别为两个整数长度)
空间复杂度:O(l1+l2)
class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.length();
int n2 = num2.length();
if(n1 == || n2 == ) return "";
int upto = ;
int sumupto = ;
string sum;
int s = ;
sum.resize(n1+n2, '');
int i, j;
for (i = n1-; i >= ; i--)
{
int a = num1[i] - '';
//注意:每次新开始upto进位值都要清零
for (j = n2-, upto = ; j >= ; j--)
{
int b = num2[j] - '';
s = b * a + upto;
upto = s / ;
//注意:要系统分析,先计算出乘法,处理好,之后再处理加法。
int rmd = s%;
int sij1 = sum[i+j+] - '';
int rs = rmd + sij1 + sumupto;
sumupto = rs/;
rs %= ;
sum[i+j+] = rs + '';
}
//注意:把最后一次的进位值加上!
//注意:要把加法进位和乘法进位都加上
sum[i+j+] += (upto+sumupto);
//注意:加法进位一定需要清零
sumupto = ;
}
while (sum.length() > && sum[] == '') sum.erase(sum.begin());
return sum;
}
};
上面是网上的答案,各种进位好凌乱,其实不需要这么复杂,对于每一位,把相乘的结果加上上一次的进位结果加上原来这个位置就有的数,然后统一计算下一次的进位。这样做,简洁的多了,也好理解多了,代码如下:
(面试腾讯实习生的时候遇到这道题,所以又重做了一遍,我在编译器中用的是strNum1, strNum2,所以在leetcode中直接赋值了)
class Solution {
public:
string multiply(string num1, string num2) {
string strNum1, strNum2;
strNum1=num1;
strNum2=num2;
int strLen1 = strNum1.size();
int strLen2 = strNum2.size();
if (strLen1 <= || strLen2 <= )
return “”;
string res(strLen1 + strLen2, '');
int carryM = ;
int carryP = ;
int i, j;
for ( i = strLen1 - ; i >= ; i--)
{
int sNum1 = strNum1[i] - '';
for ( j = strLen2 - ; j >= ; j--)
{
int sNum2 = strNum2[j] - '';
int sum = sNum1*(strNum2[j] - '') + carryP + (res[i + j + ] - '');
carryP = sum / ;
int num = sum % ;
res[i + j + ] = num + '';
}
res[i + j + ] = carryP+'';
carryP = ;
}
while (res.length() > && res[] == '') res.erase(res.begin());
return res;
}
};
Multiply Strings——面试题的更多相关文章
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- 【LeetCode练习题】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
随机推荐
- mysql jdbc 连接url
jdbc url连接地址格式: jdbc:mysql://[host][,failoverhost...][:port]/[database] [?propertyName1][=propertyVa ...
- bzoj 4831 [Lydsy1704月赛]序列操作 dp
[Lydsy1704月赛]序列操作 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 203 Solved: 69[Submit][Status][Dis ...
- HDU3231拓扑排序
Box Relations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 爬虫服务集群处理nginx返回504
最近在对爬虫服务做分布式服务的时候总是遇到服务器返回504,搞了两天才发现原来是nginx中有对超时的设置参数,自己都是用默认的,然而客户端的等待时间超过了nginx默认的超时设置 修改 keepal ...
- WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因
WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterPrope ...
- Codeforces Round #380 (Div. 2)/729D Sea Battle 思维题
Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the ...
- Cppcheck代码分析下
1.流解析 解析函数中的可能的代码执行流, 函数实际执行中只会执行代码流中的一条流 分析: 分支语句 if-else ,switch-case 循环语句 while, do-while ...
- bzoj 1036: [ZJOI2008]树的统计Count——树链剖分
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- 使用idea+Tomcat搭建servlet服务器
1.使用java 搭建一个简单的Servlet 服务器 https://blog.csdn.net/qq_35164169/article/details/76146655
- feign hystrix 线程池伸缩控制
当前使用的版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...