当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- Selenium:弹窗处理
- typescript21 - Comparison of Interfaces and Type Aliases
- vim configuration + ctag is as easy to read code as source insight
- typescript24 - type inference
- 微信小程序获取手机号phonenumber.getPhoneNumber接口开发
- 华为Android开发面试后得出的面试秘诀
- SL-12/2过流继电器
- Challenge 52 days to memorize Peppa Pig (Day 01)
- ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
- typescript27-枚举类型呢
猜你喜欢

y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)

typescript27 - what about enumeration types

Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums

状态压缩dp

HJS-DE1/2时间继电器

Risk strategy important steps of tuning method

typescript23-元组

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

pytorch、tensorflow对比学习—张量

UE4 rays flashed from mouse position detection
随机推荐
Error: AttributeError: module 'matplotlib' has no attribute 'figure'
WPF项目-初步了解数据绑定 binding
Selenium:上传、下载文件
MySQL实践总结-
High Numbers | 【Re-integration】Line Area Score 880 Examples
Li Chi's work and life summary in July 2022
UE4 从鼠标位置射出射线检测
The method of solving stored procedure table name passing through variable in mysql
ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
USB3.0:VL817Q7-C0的LAYOUT指南(二)
Asynchronous reading and writing of files
Selenium:表单切换
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
初识shell脚本
Immutable
[target detection] YOLOv7 theoretical introduction + practical test
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
UE4 rays flashed from mouse position detection