当前位置:网站首页>leetcode43 string multiplication
leetcode43 string multiplication
2022-08-01 05:19:00 【old fish 37】
class Solution {
public:
string multiply(string num1, string num2) {
vector<int>a,b,c;
//Put both arrays into the container
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');
}
//运算 Because it operates from the ones digit,So turn them upside down
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;
}
}
//Because it's upside down,So to get the last one is not0的头
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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- I met a shell script
- 华为Android开发面试后得出的面试秘诀
- 【MySQL必知必会】 表的优化 | 充分利用系统资源
- Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
- 万字逐行解析与实现Transformer,并进行德译英实战(三)
- Selenium:弹窗处理
- MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
- Selenium: element positioning
- Excel record of integer programming optimization model to solve the problem
- 2022.7.27好题选讲
猜你喜欢
Robot_Framework:关键字
牛客多校2022第四场A,H,K,N
Robot_Framework: Assertion
类神经网络训练不起来怎么办
USB3.0:VL817Q7-C0的LAYOUT指南(三)
AspNet.WebApi.Owin 自定义Token请求参数
2022.7.26 模拟赛
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
typescript26 - literal types
MySQL-DML language-database operation language-insert-update-delete-truncate
随机推荐
七、MFC序列化机制和序列化类对象
typescript28 - value of enumeration type and data enumeration
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
Robot_Framework: keyword
DL-31/6电流继电器
万字逐行解析与实现Transformer,并进行德译英实战(三)
SL-12/2过流继电器
NDK does not contain any platforms problem solving
(2022牛客多校四)N-Particle Arts(思维)
Check控件
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
Hunan institute of technology in 2022 ACM training sixth week antithesis
Selenium:弹窗处理
Selenium: element judgment
WPF项目-初步了解数据绑定 binding
关于给Qt做一个软件初始化的进度条
leetcode43 字符串相乘
typescript27 - what about enumeration types
Robot_Framework:断言
力扣(LeetCode)212. 单词搜索 II(2022.07.31)