当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- 请求/响应拦截器写法
- Selenium:浏览器操作
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
- II. Binary tree to Offer 68 - recent common ancestor
- Selenium: Dropdown Box Actions
- leetcode125 验证回文串
- WPF入门项目必知必会-初步了解数据绑定 binding
- Error: AttributeError: module 'matplotlib' has no attribute 'figure'
- WPF项目-初步了解数据绑定 binding
猜你喜欢

对话MySQL之父:一个优秀程序员可抵5个普通程序员

Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation

【MySQL必知必会】 表的优化 | 充分利用系统资源

可视化全链路日志追踪

Selenium: Popup Handling

剑指 Offer 68 - II. 二叉树的最近公共祖先

Pyspark Machine Learning: Vectors and Common Operations

华为Android开发面试后得出的面试秘诀

typescript23-tuple

leetcode43 字符串相乘
随机推荐
matplotlib pyplot
剑指 Offer 68 - II. 二叉树的最近公共祖先
LeetCode 9. 回文数
Challenge 52 days to memorize Peppa Pig (Day 01)
MySQL实践总结-
vim配置+ctag像source insight一样方便阅读代码
2022年湖南工学院ACM集训第六次周测题解
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
The method of solving stored procedure table name passing through variable in mysql
2022年超全的Android面经(附含面试题|进阶资料)
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
PAT serie b write the number 1002
Selenium:简介
USB3.0:VL817Q7-C0的LAYOUT指南(三)
I met a shell script
torch
Selenium: form switching
PAT乙级 1002 写出这个数
Robot_Framework: commonly used built-in keywords
Selenium:浏览器操作