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. RHEL6.4上Samba/NFS服务器简单配置

    近期在RHEL6.4上尝试搭建一个NAS,底层使用XFS文件系统,对外主要提供samba协议和NFS协议共享,这里把主要步骤记录下来. 环境:RHEL6.4,IP:192.168.50.117 1.关 ...

  2. 位bit,字节byte,K,M,G(转)

      字节是由8个位所组成,可代表一个字符(A~Z).数字(0~9).或符号(,.?!%&+-*/),是内存储存数据的基本单位.1 byte = 8 bit 1 KB = 1024 bytes1 ...

  3. win10忘记wifi记录

    1.点击桌面右下角无线图标 2.点击网络设置 3.点击管理WIFI设置 4.点击要管理的账户,忘记或者共享该wifi.

  4. C3P0连接池工具类实现步骤及方法

    C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...

  5. env - 在重建的环境中运行程序

    SYNOPSIS(总览) env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] DESCRIPTION(描述) 设置环境中的每个NAME为VAL ...

  6. 错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java.net.BindException: Address already in use: JVM_Bind

    在使用SpringMVC测试的时候, 遇到了这样一个问题, 说的是端口已经被使用了. 代理抛出异常错误: java.rmi.server.ExportException: Port already i ...

  7. git 添加外部项目地址

    github 提交第三方模块流程   // git config --global user.name 'your name' 可以设置全局用户名,在commit记录里显示的是这个配置设置的名称. / ...

  8. m3u8 格式转MP4

    现在很多视频网站采用HLS流媒体的方式来提供视频直播,在HTML源代码中flash的播放地址为 http://xxxxxx/video/movie.m3u8 1.m3u8下载的格式大致如下: #EXT ...

  9. css 浮动规则

    1. 如果浮动元素的高度大于父级元素,那么浮动元素块会超过父级元素的底部.解决办法:将父级元素也设置为浮动定位. 2. 不改变box尺寸的情况下增加box内部的内边距:box-sizing: bord ...

  10. POJ-1163 递推

    代码很容易看明白,就不详解了. 这个是空间优化的代码. #include <iostream> #include <algorithm> #define MAX 101 usi ...