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 ...
随机推荐
- async的基本用法
1. async函数的基本形式 //函数声明 async function foo() {} //函数表达式 const foo = async function () {}; //对象的方法 let ...
- BZOJ1912 APIO2010 洛谷P3629 巡逻
Description: 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到达其 他任 ...
- 【状压DP】【UVA11825】 Hackers' Crackdown
传送门 Description 你是一个hacker,侵入了一个有着n台计算机(编号为1.2.3....n)的网络.一共有n种服务,每台计算机都运行着所有服务.对于每台计算机,你都可以选择一项服务,终 ...
- libudev使用说明书
转http://blog.csdn.net/coroutines/article/details/38067805 1. 初始化 首先调用udev_new,创建一个udev library conte ...
- CentOS 6.5 下安装配置 mysql
如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--Cen ...
- Android 一些系统参数的获取
//获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...
- C# 枚举的初始化
3.2 枚举类型(Enum types)的默认值 对于枚举类型(Enum types),.NET会自动将字面值0(literal 0)隐式地转换为对应的枚举类型. 3.2.1 有一个0值成员 如果枚举 ...
- python---aiohttp的使用
1.aiohttp的简单使用(配合asyncio模块) import asyncio,aiohttp async def fetch_async(url): print(url) async with ...
- MySQL:Can't create test file XXX.lowe-test
问题说明 今天部署MySQL,在使用mysql_install_db,初始化数据库时报如下错误 180622 11:36:38 mysqld_safe Starting mysqld daemon w ...
- C++之容器(关联容器)
关联容器和顺序容器的本质区别:关联容器是通过键存取和读取元素.顺序容器通过元素在容器中的位置顺序存储和访问元素.因此,关联容器不提供front.push_front.pop_front.back.pu ...