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;
}
};

最新文章

  1. 简单MVC项目搭建--Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4
  2. zoj 3261 Connections in Galaxy War
  3. Java NIO框架Mina、Netty、Grizzly介绍与对比(zz)
  4. Session入门
  5. Long与long的比较
  6. redis其他问题
  7. Asp.net中前台javascript与后台C#交互
  8. 安装vs2015的时候出现的各种 1402错误
  9. http://www.linux-commands-examples.com/xmllint
  10. Android 调用jepg库进行图片压缩,保持图片不失真
  11. iOS开发之判断用户是否打开APP通知开关
  12. Azure Automation Deploy (ARM)
  13. 企业级数据库监控利器Lepus
  14. Java Web项目(Extjs)报错四
  15. How tomcat works 读书笔记十四 服务器组件和服务组件
  16. 再谈async与await
  17. ZJOI2019二轮游记
  18. Chapter 4 Invitations——1
  19. 如何把string转换char*类型
  20. maven 编译插件指定jdk版本的两种方式

热门文章

  1. JavaBean出现的目的
  2. oracle中的sql%rowcount,sql%found、sql%notfound、sql%rowcount和sql%isopen
  3. 您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
  4. dwz 在dialog里打开dialog
  5. 解决 Mac Pro 用 Excel 打开 CSV 文件不能正常显示的问题
  6. 如何在CentOS 6.5上安装EPEL 源
  7. Android开发资源推荐第2季
  8. Android开发学习笔记--给一个按钮定义事件
  9. iOS开发网络篇—大文件的多线程断点下载
  10. Mysql字段操作—增加字段、删除字段、修改字段名、修改字段类型(约束条件)