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
 
  1. class Solution {
  2. public:
  3. string multiply(string num1, string num2) {
  4.  
  5. if(num1==""||num2=="") return "";
  6.  
  7. while(num1[]=='') num1.erase();
  8. while(num2[]=='') num2.erase();
  9.  
  10. int n1=num1.size();
  11. int n2=num2.size();
  12.  
  13. int *numArr1=new int[n1];
  14. int *numArr2=new int[n2];
  15.  
  16. int *result=new int[n1+n2];
  17. memset(result,,sizeof(int)*(n1+n2));
  18. for(int i=;i<n1;i++) numArr1[i]=num1[i]-'';
  19. for(int i=;i<n2;i++) numArr2[i]=num2[i]-'';
  20.  
  21. for(int i=;i<n1;i++)
  22. {
  23. for(int j=;j<n2;j++)
  24. {
  25. result[i+j+]+=numArr1[i]*numArr2[j];
  26. }
  27. }
  28.  
  29. int carry=;
  30. for(int i=n1+n2-;i>=;i--)
  31. {
  32. result[i]+=carry;
  33. if(result[i]>=)
  34. {
  35. carry=result[i]/;
  36. result[i]=result[i]%;
  37. }
  38. else
  39. {
  40. carry=;
  41. }
  42. }
  43.  
  44. string resultStr;
  45. int k=;
  46. while(result[k]==) k++;
  47.  
  48. for(int i=k;i<n1+n2;i++)
  49. {
  50. resultStr.push_back(result[i]+'');
  51. }
  52.  
  53. return resultStr;
  54. }
  55. };

【leetcode】Multiply Strings的更多相关文章

  1. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. 【Leetcode】【Medium】Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  3. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  4. 【leetcode】Isomorphic Strings(easy)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. 【LeetCode43】 Multiply Strings

    题目描述: 解题思路: java代码: public class LeetCode43 { public static void main(String[] args) { String num1=& ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

随机推荐

  1. CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)

    CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL) 一.准备篇: /etc/init.d/iptables stop #关闭防火墙 关闭SELINUX vi /etc/sel ...

  2. springmvc请求参数获取的几种方法

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  3. C# Monitoring-network

    http://www.codeproject.com/Articles/6259/Monitoring-network-speed

  4. 史上最全的maven pom.xml文件教程详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. 【转】Kafka实战-Flume到Kafka

    Kafka实战-Flume到Kafka Kafka   2015-07-03 08:46:24 发布 您的评价:       0.0   收藏     2收藏 1.概述 前面给大家介绍了整个Kafka ...

  6. Linux查看系统信息的一些命令及查看已安装软件包的命令

    转自:http://cheneyph.iteye.com/blog/824746 系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看 ...

  7. 控制器View的生命周期及相关函数是什么?你在开发中是如何用的?

    * 1.首先判断控制器是否有视图,如果没有就调用loadView方法创建:通过storyboard或者代码: * 2.随后调用viewDidLoad,可以进行下一步的初始化操作:只会被调用一次: * ...

  8. Access应用笔记<三>

    在搭了一个数据库后,买了一本<让excel飞>,里面有提及access 经过研究之后,发现access+excel的结合确实能解决我的大部分难题,并且可以做得更好. 比较好的方法是, ac ...

  9. memcache安装

    windows下访问 http://pecl.php.net/package/memcache/3.0.8/windows 下载对应版本memcache的dll文件添加到php目录ext下 PHP.i ...

  10. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...