【leetcode】Multiply Strings
2024-09-28 22:33:12
Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
各个位相乘,保存在数组中,最后再处理进位。
如
123*456
4,5,6
8,10,12
12,15,18
________________
4,13,28,27,18
即56088
class Solution {
public:
string multiply(string num1, string num2) { if(num1==""||num2=="") return ""; while(num1[]=='') num1.erase();
while(num2[]=='') num2.erase(); int n1=num1.size();
int n2=num2.size(); int *numArr1=new int[n1];
int *numArr2=new int[n2]; int *result=new int[n1+n2];
memset(result,,sizeof(int)*(n1+n2));
for(int i=;i<n1;i++) numArr1[i]=num1[i]-'';
for(int i=;i<n2;i++) numArr2[i]=num2[i]-''; for(int i=;i<n1;i++)
{
for(int j=;j<n2;j++)
{
result[i+j+]+=numArr1[i]*numArr2[j];
}
} int carry=;
for(int i=n1+n2-;i>=;i--)
{
result[i]+=carry;
if(result[i]>=)
{
carry=result[i]/;
result[i]=result[i]%;
}
else
{
carry=;
}
} string resultStr;
int k=;
while(result[k]==) k++; for(int i=k;i<n1+n2;i++)
{
resultStr.push_back(result[i]+'');
} return resultStr;
}
};
最新文章
- 简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4
- zoj 3261 Connections in Galaxy War
- Java NIO框架Mina、Netty、Grizzly介绍与对比(zz)
- Session入门
- Long与long的比较
- redis其他问题
- Asp.net中前台javascript与后台C#交互
- 安装vs2015的时候出现的各种 1402错误
- http://www.linux-commands-examples.com/xmllint
- Android 调用jepg库进行图片压缩,保持图片不失真
- iOS开发之判断用户是否打开APP通知开关
- Azure Automation Deploy (ARM)
- 企业级数据库监控利器Lepus
- Java Web项目(Extjs)报错四
- How tomcat works 读书笔记十四 服务器组件和服务组件
- 再谈async与await
- ZJOI2019二轮游记
- Chapter 4 Invitations——1
- 如何把string转换char*类型
- maven 编译插件指定jdk版本的两种方式
热门文章
- JavaBean出现的目的
- oracle中的sql%rowcount,sql%found、sql%notfound、sql%rowcount和sql%isopen
- 您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
- dwz 在dialog里打开dialog
- 解决 Mac Pro 用 Excel 打开 CSV 文件不能正常显示的问题
- 如何在CentOS 6.5上安装EPEL 源
- Android开发资源推荐第2季
- Android开发学习笔记--给一个按钮定义事件
- iOS开发网络篇—大文件的多线程断点下载
- Mysql字段操作—增加字段、删除字段、修改字段名、修改字段类型(约束条件)