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 ...
随机推荐
- 2-SAT入门
大概学了一下2-SAT,写了一道模板和一道USACO 输出一个方案的话,tarjan缩点后倒着拓扑,染色输出. 求任何解下选哪个就得枚举每个点dfs来判断选哪个. HIT 1917(2-sat模板) ...
- PHP导出excel,无乱码
php部分 header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes&qu ...
- 背景建模技术(五):视频捕获(VideoCapture)模块
本次对“视频捕获(VideoCapture)模块”做出分析,给出源代码和对应的程序流程框架. 视频捕获模块的主要功能是设置视频或相机参数,并读取设置配置参数,最后进入帧处理模块的process进程,该 ...
- cuda环境下安装opencv出现nvcc warning : The 'compute_11'
警告打印: nvcc warning : The 'compute_11', 'compute_12', 'compute_13', 'sm_11', 'sm_12', and 'sm_13' arc ...
- E. Border
E. Border time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- bzoj 1221 [HNOI2001] 软件开发 费用流
[HNOI2001] 软件开发 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1938 Solved: 1118[Submit][Status][D ...
- matlab向量的排序(自写函数)
function a_ed = arraysort(a) %冒泡排序法 for i =1:length(a)-1 %进行多少次比较 for j=1+i:length(a) %每次求出最大的数,放在最后 ...
- [洛谷P3763] [TJOI2017]DNA
洛谷题目链接:[TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其 ...
- elasticsearch client
你可以用client做很多事情: 在集群中执行index, get, delete, search,update 操作 在集群中执行administrative tasks 如果你想再程序中嵌入ela ...
- MySQL和Postgresql的区别
一.PostgreSQL相对于MySQL的优势 1.在SQL的标准实现上要比MySQL完善,而且功能实现比较严谨:2.存储过程的功能支持要比MySQL好,具备本地缓存执行计划的能力:3.对表连接支持较 ...