当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- WPF项目-按着键盘方向键,移动格子盒子效果
- Malicious attacks on mobile applications surge by 500%
- A,H,K,N
- [target detection] YOLOv7 theoretical introduction + practical test
- y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
- Selenium:弹窗处理
- 图片更新之后Glide加载依旧是原来的图片问题
- 中国的机器人增长
- 4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
- JWL-11/2-99.9A电流继电器
猜你喜欢
中国的机器人增长
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
leetcode43 字符串相乘
2022年湖南工学院ACM集训第六次周测题解
pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
MySQL-DML language-database operation language-insert-update-delete-truncate
[target detection] YOLOv7 theoretical introduction + practical test
Power button (LeetCode) 212. The word search II (2022.07.31)
SL-12/2过流继电器
随机推荐
Seleniu: Common operations on elements
NDK does not contain any platforms问题解决
Selenium:元素定位
(2022牛客多校四)H-Wall Builder II(思维)
II. Binary tree to Offer 68 - recent common ancestor
(2022牛客多校四)N-Particle Arts(思维)
LeetCode 1189. “气球” 的最大数量
Selenium: element judgment
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
用位运算为你的程序加速
牛客多校2022第四场A,H,K,N
API Design Notes: The pimpl trick
万字逐行解析与实现Transformer,并进行德译英实战(一)
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
NUMPY
Logitech Mouse Experience Record
pytroch、tensorflow对比学习—专栏介绍
Robot_Framework: keyword
Selenium:弹窗处理