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.
要求:字符串表示的数字可能无穷大,并且非负。
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(字符串乘法模拟,包含了加法模拟)的更多相关文章
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- LeetCode OJ:Multiply Strings (字符串乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- 43. Multiply Strings 字符串表示的大数乘法
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
随机推荐
- java (给出年月日,计算该日是该年的第n天 )
package com.ywx.testdemo01; import java.util.Scanner; /** * 题目:给出年月日,计算该日是该年的第n天 * @author yangwenxu ...
- 【HEVC帧间预测论文】P1.7 Content Based Hierarchical Fast Coding Unit Decision Algorithm
Content Based Hierarchical Fast Coding Unit Decision Algorithm For HEVC <HEVC标准介绍.HEVC帧间预测论文笔记> ...
- MATLAB GUI制作快速入门
创建空白的GUI在MATLAB命令行中输入guide新建GUI,选择Blank GUI (Default),点击确定后就生成了一个空白的GUI制作界面,如下图所示 图1制作GUI的具体过程简单加法器将 ...
- Python——集合与字典练习
集合与字典练习question1问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最 ...
- 关于Ubuntu 16.04中E: Could not get lock /var/lib/dpkg/lock - open的三种解决方案
问题 在Ubuntu中,有时候运用sudo apt-get install 安装软件时,会出现如下的情况: E: Could not get lock /var/lib/dpkg/lock - op ...
- powerDesigner的name和comment转化
name2comment.vbs '****************************************************************************** '* ...
- C++关键字(保留字)
C++ 关键字 点击下表以进入具体释义 __abstract 2 __alignof Operator __asm __assume __based __box 2 __cdecl __declspe ...
- P2756 网络流解决二分图最大匹配
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 P2756 飞行员配对方案问题 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语 ...
- Oracle数据库的基本使用
1.Linux安装 (略) 2.Oracle数据库监听命令: 监听状态:$lsnrctl status 启动监听:$lsnrctl start 关闭监听:$lsnrctl stop 重载监听:$l ...
- SpringCloud版本介绍和SpringBoot的兼容性
Spring Cloud是一个由众多独立子项目组成的大型综合项目,每个子项目有不同的发行节奏,都维护着自己的发布版本号.Spring Cloud通过一个资源清单BOM(Bill of Material ...