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.

要求:字符串表示的数字可能无穷大,并且非负。

class Solution {
private:
vector<string> tempStrs;
public:
string add(string num1,string num2)
{
int n1=num1.size();
int n2=num2.size();
int i=n1-;int j=n2-;
string resStr;
int jinwei=;
while (i>=&&j>=)
{
int temp=(num1[i]-'')+(num2[j]-'')+jinwei;
jinwei=temp>=?:;
temp%=;
resStr.push_back(temp+'');
--i;
--j;
}
if(i>=){
while (i>=)
{
int temp=(num1[i]-'')+jinwei;
jinwei=temp>=?:;
temp%=;
resStr.push_back(temp+'');
--i;
}
}
else if(j>=){
while (j>=)
{
int temp=(num2[j]-'')+jinwei;
jinwei=temp>=?:;
temp%=;
resStr.push_back(temp+'');
--j;
}
}
if(jinwei!=){
resStr.push_back(jinwei+'');
}
reverse(resStr.begin(),resStr.end());
return resStr;
}
string multiply(string num1, string num2) {
if(num1.empty()||num2.empty()) return "";
if(num1==""||num2=="") return "";
int jinwei=;
int n1=num1.size();
int n2=num2.size();
if(n2>n1){
string temp=num1;
num1=num2;
num2=temp;
n1=num1.size();
n2=num2.size();
}
int time=;
string tempStr;
string res;
for(int i=n2-;i>=;--i)
{
tempStr.clear();
jinwei=;
for(int j=n1-;j>=;--j)
{
int temp=(num1[j]-'')*(num2[i]-'')+jinwei;
jinwei=temp>=?(temp/):;
temp=temp%;
tempStr.push_back(temp+'');
}
if(jinwei!=){
tempStr.push_back(jinwei+'');
}
reverse(tempStr.begin(),tempStr.end());
for(int q=;q<time;++q){
tempStr.push_back('');
}
tempStrs.push_back(tempStr);
++time;
}
if(tempStrs.size()>){
res=add(tempStrs[],tempStrs[]);
for (int i=;i<tempStrs.size();++i)
{
res=add(res,tempStrs[i]);
}
return res;
}else
{
return tempStrs[];
} }
};

Multiply Strings(字符串乘法模拟,包含了加法模拟)的更多相关文章

  1. 【LeetCode每天一题】Multiply Strings(字符串乘法)

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

  2. LeetCode OJ:Multiply Strings (字符串乘法)

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

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

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

  4. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  5. 43. Multiply Strings 字符串表示的大数乘法

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

  6. [LeetCode] Multiply Strings 字符串相乘

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

  7. [Leetcode] Multiply strings 字符串对应数字相乘

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

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

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

  9. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

随机推荐

  1. VBA 连接sql server的用法

    cnnstr = "Provider=sqloledb;Data Source=192.211.21.8;Initial Catalog=pub;UID=账号;PWD=密码" VB ...

  2. isEqual ,判断两个对象或变量是否相等

    function isEqual(a, b) { //如果a和b本来就全等 if (a === b) { //判断是否为0和-0 return a !== 0 || 1 / a === 1 / b; ...

  3. C ++ _基础之共用体

    由以下代码来进一步学习共用体 #include <stdio.h> #include<iostream> void main() { union un { int a; cha ...

  4. 4.关于while循环的基础小练习

    1)使用while.if循环输入123456 8910 count = 0 while count < 10: count += 1 if count == 7: print('') else: ...

  5. HTTP请求头的具体含意

    为你详细解读HTTP请求头的具体含意 | 浏览:5763 | 更新:2012-03-16 16:41 当我们打开一个网页时,浏览器要向网站服务器发送一个HTTP请求头,然后网站服务器根据HTTP请求头 ...

  6. poj-1163 动态规划

    这道题目并不能直接使用递归,因为 7(1) 7(1)         7(1) 7(1)      7(2)         7(1) 7(1)       7(3)         7(3)    ...

  7. curl 模拟post请求

    curl -H "Content-Type:application/json" -X POST --data '{"openId":"xxxxxxx& ...

  8. Linux磁盘管理及Lvm

    1. 硬盘接口 IDE: SATA:常用: SCSI:主要用于高端服务器,linux默认: SAS 2. 硬盘种类 SATA硬盘:串口硬盘,有较强的纠错能力: SCSI 硬盘:默认硬盘: SAS 硬盘 ...

  9. Go:json(序列化、反序列化)

    一.示例 package main import ( "encoding/json" "fmt" ) type Person struct { Name str ...

  10. CentOS6.8下安装Docker

    原文章链接https://www.cnblogs.com/baolong/p/5743420.html. 由于在自己安装的虚拟机上打开linux终端命令行输入uname -a 以及cat /etc/r ...