当前位置:网站首页>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;
}
};
如有更好想法,欢迎留言!多多指教!
边栏推荐
- The method of solving stored procedure table name passing through variable in mysql
- 【MySQL必知必会】 表的优化 | 充分利用系统资源
- High Numbers | 【Re-integration】Line Area Score 880 Examples
- Selenium: Element wait
- The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
- Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
- Selenium: Dropdown Box Actions
- Selenium:操作JS
- Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
- 混合型界面:对话式UI的未来
猜你喜欢
随机推荐
Challenge 52 days to memorize Peppa Pig (Day 01)
Selenium:下拉框操作
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
MySQL Practice Summary -
混合型界面:对话式UI的未来
2022年超全的Android面经(附含面试题|进阶资料)
Robot_Framework:常用内置关键字
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
Selenium:上传、下载文件
Li Chi's work and life summary in July 2022
typescript27-枚举类型呢
Immutable
typescript26 - literal types
万字逐行解析与实现Transformer,并进行德译英实战(一)
MySQL-数据定义语言-DDLdatebase define language
USB3.0:VL817Q7-C0的LAYOUT指南(三)
LeetCode 1189. “气球” 的最大数量
(2022牛客多校四)N-Particle Arts(思维)
typescript23-tuple









