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. 2014第3周三JS进阶书籍

    本来想尝试每天回答或看已解决的3个问题来学习总结今天的知识点,看了下博文里面的问答,在问的和已解决的都提不起兴趣.就看了下知识库里面一些文章,把里面感觉好的段落再摘录一下,为自己再看时备忘. 第一阶段 ...

  2. POJ22230 Watchcow (欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6477   Accepted: 2823   Specia ...

  3. Ehcache RIM

    Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示:   Ehcache支持多种集群方式,下面以RMI通信方式为例,来具 ...

  4. JS~JS里的数据类型

    JS里的数据类型,它虽然是个弱类型的语言,但它也有自己的规定的,它不会向其它语言那么,使用int来声明一个整形变量,而是使用 var,如果你是一个C#的开发者,你就会知道,原来C#现在也在和JS学,开 ...

  5. 【DataStructure】Some useful methods about linkedList(二)

    Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is { ...

  6. MFC多线程内存泄漏问题&amp;解决方法

    在用visual studio进行界面编程时(如MFC),前台UI我们能够通过MFC的消息循环机制实现.而对于后台的数据处理.我们可能会用到多线程来处理. 那么对于大多数人(尤其是我这样的菜鸟),一个 ...

  7. NodeJS初学者实战之旅(I) —— 介绍、目录

    旅行之初 作为一个工作了两年多后端开发人员,未接触过NodeJS,对它的认知也仅仅停留在“可以使用JavaScript代码编写服务端程序”上.最近空闲时间 较多,便想来啃它一啃.但是没有一个实际的玩意 ...

  8. [转载]提升进程权限-OpenProcessToken等函数的用法

    GetCurrentProcessID 得到当前进程的ID OpenProcessToken 得到进程的令牌句柄LookupPrivilegeValue 查询进程的权限 AdjustTokenPriv ...

  9. Java IO 和 NIO

    昨天面试问到了有关Java NIO的问题,没有答上来.于是,在网上看到了一篇很有用的系列文章讲Java IO的,浅显易懂.后面的备注里有该系列文章的链接.内容不算很长,需要两个小时肯定看完了,将该系列 ...

  10. mysql数据备份和还原命令

    mysql数据库备份和还原   备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfil ...