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. Python打印格式化与字符串

    关于Python打印格式化与字符串,比较全面的总结,希望对大家有帮助~ # -*- coding: cp936 -*- ''' 打印格式 ''' print "a" print & ...

  2. 给ie浏览器添加推荐浏览器提示

    <script type="text/javascript"> var isIE = !!window.ActiveXObject; var isIE6 = isIE ...

  3. 百度地图V1.5 LocalSearch增加浮动窗体的操作内容

     1.初始化LocalSearch控件 LocalSearch = new BMap.LocalSearch(map, { renderOptions : { map : map, panel : & ...

  4. python选择排序实现与C选择排序实现

    python代码: #coding=utf-8 if __name__=="__main__": arr=[3,2,1,7,11,4,5,8] print "Before ...

  5. ZOJ3578(Matrix)

    Matrix Time Limit: 10 Seconds      Memory Limit: 131072 KB A N*M coordinate plane ((0, 0)~(n, m)). I ...

  6. wxpython StatuBar 带进度条的状态栏

    # -*- coding: utf- -*- import wx class customStatusBar(wx.StatusBar): def __init__(self, parent): wx ...

  7. Unity 使用实体类

    故事的由来: 正在开发打飞机的游戏,遇到这样的数据结构,游戏有很多关卡-> 每个关卡有几波怪物->每一波里面有怪物和数量 [] 关卡 { []波{ {怪物,数量},{怪物,数量},{怪物, ...

  8. Unity编辑器扩展Texture显示选择框

    学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=. Unity开发了组件Inspector视图扩展API,如果我们要写插 ...

  9. QUARTZ CRON

    本文来自:http://www.blogjava.net/crazycy/archive/2013/06/06/400287.html 每次使用Quartz Cron的时候都要去查manual doc ...

  10. 怎样使用jetty

    一直都听说jetty跟Tomcat一样,是一个web容器.之前做项目的时候,也使用过jetty,只是当时jetty是作为一个插件,跟maven集成使用的.那个时候,因为是第一次使用jetty,感觉je ...