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 ...
随机推荐
- bzoj1057: [ZJOI2007]棋盘制作(悬线法)
题目要求纵横坐标和奇偶性不同的点取值不同,于是我们把纵横坐标和奇偶性为1的点和0的点分别取反,就变成经典的最大全1子矩阵问题了,用悬线法解决. #include<iostream> #in ...
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...
- BAT-Python面试题
Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def fun(a): a = 2 fun(a) print(a) # 1 代码二: a = ...
- 优化Hadoop Balancer运行速度
(如果运行hbase的话建议为16384),指定用于在DataNode间传输block数据的最大线程数,老版本的对应参数为dfs.datanode.max.xcievers 2.修改dfs.datan ...
- oracle重新编译失效对像
重新编译失效对像可执行utlrp.sql文件: SQL> @?/rdbms/admin/utlrp.sql TIMESTAMP --------------------------------- ...
- Asp.net Web Api 2 FORM Authentication Demo
最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...
- 【BZOJ2882】工艺 [SAM]
工艺 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 小敏和小燕是一对好朋友. 他们正在玩一 ...
- Linux while 和 read 的用法
Reference: [ linux man doc ] [ CSDN roler_ ] [ Reads from the file descriptor] read 命令说明 SYNTAX : re ...
- error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'
error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...
- eclipse+EGIT+GitHub
下载EGIT:http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_EGit.3F 1.下载eclipse版本对应的E ...