题目: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.

就是实现大数乘法。嘿嘿,我先用long long直接乘试试手感,试过了不行。所以还是要用字符串表示才行。

我是这样做的,先实现两个子函数,一个是实现一个数和一个字符串的数相乘的结果。另一个是实现两个字符串相加的结果。这样,大数乘法就可以由其中一个大数的每一位和另一个大数相乘然后再相加,就是要注意分解一个大数的时候记得要在结果后面加上零。例如12乘以3,我们把12分成1和2分别和3相乘,然后1和3相乘的后面要加一个零,因为1处于12的十位数。

oj上居然用了148ms。不忍直视啊。写了那么长。。。虽然Accept了

  1. class Solution {
  2. public:
  3. string singleMulti(string s, char a) // 给定一个字符串和一个字符,返回乘积(字符串形式)
  4. {
  5. if (s.size() == )
  6. return s;
  7. if (a == '')
  8. return "";
  9. int c = a - '';
  10. int up = ;
  11. for (int i = s.size() - ; i > -; --i)
  12. {
  13. int tmp = s[i] - '';
  14. int sum_now = up + c*tmp;
  15. s[i] = sum_now% + '';
  16. up = sum_now/;
  17. }
  18. if (up != )
  19. {char tmp = up + '';
  20. s = tmp + s;}
  21. return s;
  22. }
  23. string sum42(string s1, string s2) // sum of two string
  24. {
  25. if (s1.size() == )
  26. return s2;
  27. if (s2.size() == )
  28. return s1;
  29. string s = s1;
  30. int len1 = s1.size() - , len2 = s2.size() - , up = ;
  31. while(len1 > - && len2 > -)
  32. {
  33. int n1 = s1[len1] - '';
  34. int n2 = s2[len2] - '';
  35. int n3 = n1 + n2 + up;
  36. s[len1] = n3% + '';
  37. up = n3/;
  38. len1--;len2--;
  39. }
  40. while(len1 > -)
  41. {
  42. int n1 = s1[len1] - '';
  43. int n3 = n1 + up;
  44. s[len1] = n3% + '';
  45. up = n3/;
  46. len1--;
  47. }
  48. while(len2 > -)
  49. {
  50. int n2 = s2[len2] - '';
  51. int n3 = n2 + up;
  52. char tmp = n3% + '';
  53. s = tmp + s;
  54. up = n3/;
  55. len2--;
  56. }
  57. if(up)
  58. {
  59. char tmp = up + '';
  60. s = tmp + s;
  61. }
  62. return s;
  63. }
  64. string multiply(string num1, string num2)
  65. {
  66. if (num1 == "" || num2 == "")
  67. return "";
  68. int len1 = num1.size()-;
  69. string s;
  70. int cnt = ;
  71. while(len1 > -)
  72. {
  73. string tmp = "";
  74. int tn = cnt;
  75. while(tn)
  76. {
  77. tmp = '' + tmp;
  78. tn--;
  79. }
  80. s = sum42(s,singleMulti(num2, num1[len1])+ tmp);
  81. len1--;
  82. cnt++;
  83. }
  84. return s;
  85. }
  86. };

由于自己的代码那么长,所以也学习了下别人的,例如这里利用下面的图,很好理解。将图贴出:

我们以289*758为例

按照这个图我写了下代码(跟原版主略有不同):

  1. class Solution {
  2. public:
  3. string multiply(string num1, string num2)
  4. {
  5. if (num1 == "" || num2 == "")
  6. return "";
  7. string s = "";
  8. int len1 = num1.size(), len2 = num2.size();
  9. vector<int> container(len1+len2, ); // 用来存表中红色部分值
  10.  
  11. for (int i = ; i < len1; ++i)
  12. for (int j = ; j < len2; ++j)
  13. {
  14. container[i+j] += (num1[len1 - - i]-'')*(num2[len2 - - j]-''); // 注意标号
  15. }
  16. //处理进位
  17. int up = , cnt = ;
  18. while(cnt<len1+len2)
  19. {
  20. container[cnt] += up;
  21. up = container[cnt]/;
  22. container[cnt] %= ;
  23. cnt++;
  24. }
  25. cnt--;
  26. while(cnt > - && !container[cnt])//不应该有的零去掉
  27. {
  28. cnt--;
  29. };
  30. while(cnt > -) // 输出就是结果,注意方向被搞反了
  31. {
  32. char ch = container[cnt] + '';
  33. s += ch;
  34. cnt--;
  35. }
  36. return s;
  37. }
  38. };

这个主要是注意存的方向,不要把小标弄混淆了。输出的时候也要注意,别搞反了。低于60ms过。

leetcode 第42题 Multiply Strings的更多相关文章

  1. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

  2. [LeetCode] 大数问题,相加和相乘,题 Multiply Strings

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

  3. LeetCode(43)Multiply Strings

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

  4. leetcode个人题解——#43 Multiply Strings

    思路:高精度乘法就可以了. 有两个错误以前没在意,1.成员属性定义时候不能进行初始化, vector<); 这样隐性调用了函数进行初始化的形式特别要注意,也是错误的: 2.容器类只有分配了空间时 ...

  5. LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD

    题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  8. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  9. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

随机推荐

  1. HDU1160(LIS)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:求体重下降.速度添加的样例最多有多少个 依据体重降序排一下,然后求速度的最长上升子序列 , ...

  2. LeetCode :: Insertion Sort List [具体分析]

    Sort a linked list using insertion sort. 仍然是一个很简洁的题目,让我们用插入排序给链表排序:这里说到插入排序.能够来回想一下, 最主要的入门排序算法.就是插入 ...

  3. Base64中文不能加密问题

    最近用到了Base64.js来对url参数进行加密,字母和数字都可以很好地加密/解密. 但测试中文时发现不能进行转换,貌似Base64.js不支持中文字符. 联想到encodeURI()对url的编码 ...

  4. C#和Java中执行SQL文件脚本的代码(非常有用)

    原文:C#和Java中执行SQL文件脚本的代码(非常有用) 我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析 去 ...

  5. thinkphp学习笔记9—自动加载

    原文:thinkphp学习笔记9-自动加载 1.命名空间自动加载 在3.2版本中不需要手动加载类库文件,可以很方便的完成自动加载. 系统可以根据类的命名空间自动定位到类库文件,例如定义了一个类Org\ ...

  6. tortoisegit使用密钥连接服务器(转)

    目录 [hide] 1 使用putty的密钥 1.1 生成putty密钥 2 在服务器上添加openssh公钥 3 在tortoisegit上使用密钥 4 putty密钥与openssh密钥转化 5  ...

  7. unity 编辑器和插件生产(四.2)

    上次 我们告诉编辑器制作,如何将图像加载到现场,如今 我们要告诉下.怎么样 制造UIButton以及UIimage交换. 阿土. 进入专题. 首先,我们要明白 unity机制.button属性等. 首 ...

  8. nodejs安装:nodejs入门

    nodejs开篇 前几天看到好多关于node 的帖子没有单独说明node安装的文章~ 特发此篇 总结一下平时在windows上nodejs的安装... 1.js来搞前后端分离是nodejs的一大特点, ...

  9. 用Markdown来写作

    Markdown 是一种简单的.轻量级的标记语法.github上面很多的README就是用markdonw语法写的. Markdown 的语法十分简单,常用的标记符号也不超过十个,且一旦熟悉这种语法规 ...

  10. android 性能測试iozone篇

    一:简单介绍 iozone是一个文件系统的benchmark工具, 用于測试不同的操作系统中文件系统的读写性能, 能够測试下面13种模式 0=write/rewrite 1=read/re-read ...