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的更多相关文章

  1. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  2. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  3. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

  4. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  5. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  6. Java for LeetCode 043 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. leetcode:Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  10. leetcode:Multiply Strings(字符串的乘法)【面试算法题】

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

随机推荐

  1. ARM架构和X86架构对比

    转载地址 我们就ARM架构的系统与X86架构系统的特性进行一个系统分析,方便用户在选择系统时进行理性.合理的比价分析. 一.性能: X86结构的电脑无论如何都比ARM结构的系统在性能方面要快得多.强得 ...

  2. Binary Tree Paths 解答

    Question Given a binary tree, return all root-to-leaf paths. For example, given the following binary ...

  3. LeeCode-Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. HDU4762(JAVA大数)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. initial pointer [expert c]

    initial differece between pointer and array Both arrays and pointers can be initialized with a liter ...

  6. [置顶] ID3算法的python实现

    这篇文章的内容接着http://blog.csdn.net/xueyunf/article/details/9214727的内容,所有还有部分函数在http://blog.csdn.net/xueyu ...

  7. 一个非常给力的播放器video-js

    video-js采用的是html5播放器. 在不支持html5的浏览器会自动切换成flash. video-js的官网http://www.videojs.com/ 看看下载的demo就知道个大概了. ...

  8. NTP-ntpdate:no server suitable for synchronization found

    NTP-ntpdate 问题处理 解决ntp的错误 no server suitable for synchronization found 当用ntpdate -d 来查询时会发现导致 no ser ...

  9. sybase用户管理(创建、授权、删除)

    一.登录用户管理:1.创建用户:sp_addlogin loginame, passwd [, defdb] [, deflanguage] [, fullname] [, passwdexp] [, ...

  10. Candy Bags

    读懂了题就会发现这是个超级大水题 Description Gerald has n younger brothers and their number happens to be even. One ...