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. C#创建任务计划

    因写的调用DiskPart程序是要用管理员身份运行的,这样每次开机检查都弹个框出来确认肯定不行.搜了下,似乎也只是使用任务计划程序运行来绕过UAC提升权限比较靠谱,网上的都是添加到计算机启动的,不是指 ...

  2. dircolors - 设置‘ls'显示结果的颜色

    SYNOPSIS[总览] dircolors [-b] [--sh] [--bourne-shell] [-c] [--csh] [--c-shell] [-p] [--print-database] ...

  3. Web前端基础怎么学? JavaScript、html、css知识架构图

    以前开发者只要掌握 HTML.CSS.JavaScript 三驾马车就能胜任一份前端的工作了.而现在除了普通的编码以外,还要考虑如何性能优化,如何跨端.跨平台实现功能,尤其是 AI.5G 技术的来临, ...

  4. js 跨域深入理解与解决方法

    参考博客: https://www.cnblogs.com/2050/p/3191744.html

  5. C++ new delete(一)

    在C#.Java這種managed語言,因為有garbage collection,所以完全不用考慮free()或delete,但在C/C++,有時候要delete的,有時又不用,到底哪些改delet ...

  6. C指针与数组之间的细节

    看以下代码: #include <stdio.h> void f(char**); int main() { char *argv[] = { "ab", " ...

  7. excel组装sql

    ="INSERT INTO TABLE_XXXX (COLUMN_1, COLUMN_2, COLUMN_3, COLUMN_4, COLUMN_5, COLUMN_6, COLUMN_7, ...

  8. kvm使用kickstart文件自动安装系统

    假定kvm已经准备好 1.创建磁盘 qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G 2.上传或下载安装镜像 mkdir -p /kvm/iso cd ...

  9. MySQL autocommit 和 start transaction

    autocommit 和 start transaction 都是事务相关的命令.类似MyISAM的mysql引擎就不支持. autocommit 默认是ON状态,即sql语句是自动提交的 show ...

  10. 我的java web之路(安装)

    所有的软件下载完,陪完jdk之后,迎来了一系列的安装工作... 1.安装SQL Server 2005 首先,打开ISS功能,控制面板->程序->打开或关闭windows功能 注意红框内的 ...