当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- What should I do if the neural network cannot be trained?
- (2022牛客多校四)N-Particle Arts(思维)
- Selenium:上传、下载文件
- [MySQL] 多表查询
- 【MySQL必知必会】 表的优化 | 充分利用系统资源
- The difference between scheduleWithFixedDelay and scheduleAtFixedRate
- Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
- UE4 rays flashed from mouse position detection
- 报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
- Robot_Framework:断言
猜你喜欢
随机推荐
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
SL-12/2过流继电器
NDK does not contain any platforms problem solving
typescript24 - type inference
Malicious attacks on mobile applications surge by 500%
PAT乙级 1002 写出这个数
25. 这三道常见的面试题,你有被问过吗?
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Selenium:元素判断
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
华为Android开发面试后得出的面试秘诀
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
25. Have you been asked these three common interview questions?
Selenium:上传、下载文件
About making a progress bar for software initialization for Qt
vim配置+ctag像source insight一样方便阅读代码
Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?
小心你的字典和样板代码
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (2)