当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- LeetCode 9. 回文数
- pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
- NDK does not contain any platforms问题解决
- 使用string 容器翻转 字母
- 小心你的字典和样板代码
- y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
- 七、MFC序列化机制和序列化类对象
- WPF项目-按着键盘方向键,移动格子盒子效果
- Asynchronous reading and writing of files
- MySQL-数据定义语言-DDLdatebase define language
猜你喜欢
随机推荐
NDK does not contain any platforms problem solving
typescript23-tuple
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (2)
Optional parameters typescript19 - object
对话MySQL之父:一个优秀程序员可抵5个普通程序员
About making a progress bar for software initialization for Qt
state compressed dp
typescript24 - type inference
Selenium:浏览器操作
小心你的字典和样板代码
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
2022年湖南工学院ACM集训第六次周测题解
Typescript20 - interface
MySQL-DML language-database operation language-insert-update-delete-truncate
II. Binary tree to Offer 68 - recent common ancestor
(2022 Niu Ke Duo School IV) K-NIO's Sword (Thinking)
华为Android开发面试后得出的面试秘诀
微信小程序用户登录auth.code2Session接口开发
Malicious attacks on mobile applications surge by 500%
WPF项目-按着键盘方向键,移动格子盒子效果