当前位置:网站首页>leetcode43 字符串相乘
leetcode43 字符串相乘
2022-08-01 05:11:00 【老鱼37】
class Solution {
public:
string multiply(string num1, string num2) {
vector<int>a,b,c;
//把两个数组都放进容器里面
for(int i=0;i<num1.size();i++)
{
a.push_back(num1[i]-'0');
}
for(int i=0;i<num2.size();i++)
{
b.push_back(num2[i]-'0');
}
//运算 因为从个位运算,所以把他们倒过来来
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
c.resize(a.size()+b.size());//调整c容量
//运算
for(int i=0;i<a.size();i++)
{
for(int j=0;j<b.size();j++)
{
c[i+j]+=a[i]*b[j];
c[i+j+1]+=c[i+j]/10;
c[i+j]=c[i+j]%10;
}
}
//因为是倒着放的,所以要取到最后那个不是0的头
int start=c.size()-1;
while(start>0&&c[start]==0) start--;//找到头
//创建string接收
string ans;
//将数组转化为字符串
for(int i=start;i>=0;i--)
{
ans+='0'+c[i];
}
return ans;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
猜你喜欢
USB3.0:VL817Q7-C0的LAYOUT指南(三)
JWL-11/2-99.9A电流继电器
pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
HJS-DE1/2时间继电器
(2022 Nioke Duo School IV) H-Wall Builder II (Thinking)
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
Typescript22 - interface inheritance
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
MySQL-DML language-database operation language-insert-update-delete-truncate
y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
随机推荐
Typescript20 - interface
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
High Numbers | 【Re-integration】Line Area Score 880 Examples
LeetCode 1189. “气球” 的最大数量
typescript28 - value of enumeration type and data enumeration
Immutable
WPF入门项目必知必会-初步了解数据绑定 binding
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
Robot_Framework:关键字
typescript27-枚举类型呢
mysql中解决存储过程表名通过变量传递的方法
LeetCode 387. 字符串中的第一个唯一字符
typescript24 - type inference
备战金九银十,如何顺利通过互联网大厂Android的笔面试?
state compressed dp
API Design Notes: The pimpl trick
In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
MySQL Practice Summary -
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
万字逐行解析与实现Transformer,并进行德译英实战(一)