【LeetCode练习题】Multiply Strings
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.
题目意思:
给两个string,计算string的乘积。
string中的数可以非常大并且是非负数。
(就是大数的乘法运算嘛。。。)
解题思路:
由于之前已经练习过了大数的加减乘除四则运算了,所以这道题跟那个是一样的原理。
要想实现乘法,首先得实现大数的加法,因为按照小学摆竖式计算乘法的时候需要在会加法的基础上才行。
于是我们首先实现了大数的加法,基本上就是模拟了用竖式每一位相加的过程,其中注意到产生的进制要参与高位的运算。
实现了加法后,乘法也就出来了。
代码如下:
string operator+(const string &num1,const string &num2){
int nCarry = ;
string numTemp; int i = num2.size() - ;
int j = num1.size() - ;
for(; i >= || j >= ; i--,j--){
char a,b;
if(i>=)
b = num2[i] - '';
else
b = ;
if(j>=)
a = num1[j] - '';
else
a = ;
char c = a + b + nCarry;
nCarry = c / ;
numTemp.push_back(c% + '');
} if(nCarry != ){
numTemp.push_back(nCarry + '');
} for(i = , j = numTemp.size() - ; i < j; i++,j--){
char cTemp = numTemp[i];
numTemp[i] = numTemp[j];
numTemp[j] = cTemp;
}
return numTemp;
}
string operator*(const string &num1,const string &num2){
int nCarry = ;
string numTemp;
string result; int i = num2.size()-;
for(; i >= ; i--){
char a = num2[i] - '';
int j = num1.size() - ;
for(; j >= ; j--){
char b = num1[j] - '';
char c = b * a + nCarry;
nCarry = c/;
numTemp.push_back(c % + '');
}
if(nCarry != ){
numTemp.push_back(nCarry + '');
nCarry = ;
} //reverse
int n = ;
int m = numTemp.size() - ;
for(; n < m; n++,m--){
char cTemp = numTemp[n];
numTemp[n] = numTemp[m];
numTemp[m] = cTemp;
} for(int t = num2.size() - ; t > i; t--)
{
numTemp.push_back('');
} result = result + numTemp;
numTemp.clear();
}
return result;
} class Solution {
public:
string multiply(string num1, string num2) {
string ret;
string zero = "";
if(!num1.compare(zero) || !num2.compare(zero)){
ret = "";
return ret;
}
ret = num1 * num2;
return ret;
}
};
【LeetCode练习题】Multiply Strings的更多相关文章
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode:Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
随机推荐
- WPF/ArcGIS Engine三维开发和EVC3/4升级到VS项目建议(转)
系统环境:Windows 7 专业版,Visual Studio 2010,ArcGIS Engine 9.3 1.创建项目 创建一个WPF的项目,必须选择.Net framework 3.5(AE9 ...
- C语言随笔_fopen
有同学问我,以下代码会输出“===”,为什么呀? if( (fp = fopen("data.dat","r"))==NULL){ printf("= ...
- OpenWrt编译
OpenWrt编译简单过程1,OpenWrt编译环境准备sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoco ...
- JDBC官方用法
JDBC官方用法https://bitbucket.org/xerial/sqlite-jdbc/#markdown-header-usage 代码下载https://github.com/xeria ...
- 【错误】:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
错误:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决 ...
- 国际C语言混乱代码大赛代码赏析(一)【转】
本文转载自:http://blog.csdn.net/ce123_zhouwei/article/details/9073869 国际C语言混乱代码大赛代码赏析(一) 近段时间在看<C专家编程& ...
- 使用 React 和 Flux 创建一个记事本应用
React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
</pre></center><center style="font-family: Simsun;font-size:14px;"><s ...
- 解决ie6 闪动的问题
/*解决ie6 闪动的问题*/ html,html body{_background-image:url(about:blank);_background-attachment:fixed}
- Unity IOC注入详细配置(MVC,WebApi)
一直想写一篇关于unity 详细的配置信息的文章,也算是自我总结吧 先介绍了unity , Unity是微软官方推荐使用的轻型的IOC框架,支持各种方式的注入 ,使用来解耦的利器. 获取unity 的 ...