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 ...
随机推荐
- OCP 11g 第二章练习
练习 2-1 在Windows计算机上安装SQL Developer 在本练习中,将在Windows计算机上安装SQL Developer 1. 从以下URL下载当前SQL Developer版本: ...
- NPM、nodeJS安装,grunt自动化构建工具学习总结
一:安装 npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从N ...
- dede网站目录权限设置
如果你的网站数据十分重要(那种两天就能弄好的垃圾站就算了),建议按本文所说的安全步骤进行严格的设置.1.目录权限 我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的 ...
- linux centos 中目录结构的含义
文件夹的含义 文件夹路径 含义 / 所有内容的开始 /root 系统管理员目录 /bin 缺省的liunx工具,就是存储命令的目录 环境变量等等 /etc 系统的配置 配置文件的存 ...
- TensorFlow低阶API(二)—— 张量
简介 正如名字所示,TensorFlow这一框架定义和运行涉及张量的计算.张量是对矢量和矩阵向潜在的更高维度的泛化.TensorFlow在内部将张量表示为基本数据类型的n维数组. 在编写TensorF ...
- 基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table
基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table https://segmentfault.com/a/1190000015970367
- Hydraulic Motor Manufacturers - What Is A Cycloidal Hydraulic Motor?
The Hydraulic Motor manufacturers stated that the cycloidal hydraulic motor is fixedly connec ...
- pycharm 用远程环境时报错bash: line 0: cd: /home/tmp: No such file or directory
delete redundant path
- vim全选复制
网上一堆答案全是ggyG,根本不行, 正确答案应该是 gg"*yG 或者 gg"*+yG 下面是在stack overflow 上找到的答案,亲测有效,在此记录下 stackove ...
- In line copy and paste to system clipboard
On the Wiki Wiki Activity Random page Videos Photos Chat Community portal To do Contribute Watch ...