[LeetCode] 43. Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088" 

Constraints:

  • 1 <= num1.length, num2.length <= 200
  • num1 and num2 consist of digits only.
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.

字符串相乘。

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/multiply-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意很简单,给两个以 string 表示的数字做乘法。无需考虑一些比如 string 中夹杂字母的 case,但是不允许将 input convert 成数字再做乘法。影子题415。注意如下几个地方,以帮助理解代码

  • 如果 num1 有 i 个数字,num2 有 j 个数字,最后的结果 res 最多只有 i + j 个数字
    • 举例,99x99 = 9801;99x999 = 98901
  • 细心观察之后就发现,num1[i] 和 num2[j] 的乘积最多只可能是一个两位数,对应的是 res[i+j] 和 res[i+j+1] 这两个位置,其中 res[i+j] 是十位数,res[i+j+1] 是个位数
  • (代码中)sum 变量不光是记录了当前两个数字的乘积,同时也累加了个位数上已经产生的数字
  • 但是 res[i+j] 位置上却不需要累加,直接把乘积的十位数放在这里即可
  • 最后再转换成字符串的时候注意跳过所有前缀的 0
  • 如果 StringBuilder 为空的话,直接返回 0

https://leetcode-cn.com/problems/multiply-strings/solution/gao-pin-mian-shi-xi-lie-zi-fu-chuan-cheng-fa-by-la/

JavaScript实现

 1 /**
 2  * @param {string} num1
 3  * @param {string} num2
 4  * @return {string}
 5  */
 6 var multiply = function(num1, num2) {
 7     let m = num1.length;
 8     let n = num2.length;
 9     let pos = new Array(m + n).fill(0);
10 
11     for (let i = m - 1; i >= 0; i--) {
12         for (let j = n - 1; j >= 0; j--) {
13             let mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
14             let p1 = i + j;
15             let p2 = i + j + 1;
16             let sum = mul + pos[p2];
17             pos[p1] += Math.floor(sum / 10);
18             pos[p2] = sum % 10;
19         }
20     }
21     let res = '';
22     for (let p of pos) {
23         if (!(res.length === 0 && p === 0)) {
24             res += p;
25         }
26     }
27     return res.length === 0 ? '0' : res;
28 };

 

Java实现

 1 class Solution {
 2     public String multiply(String num1, String num2) {
 3         int m = num1.length();
 4         int n = num2.length();
 5         int[] res = new int[m + n];
 6         for (int i = m - 1; i >= 0; i--) {
 7             for (int j = n - 1; j >= 0; j--) {
 8                 int product = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
 9                 int p1 = i + j;
10                 int p2 = i + j + 1;
11                 // sum首先累加了p2位置上已经有的数字
12                 int sum = product + res[p2];
13                 res[p1] += sum / 10;
14                 res[p2] = sum % 10;
15             }
16         }
17         StringBuilder sb = new StringBuilder();
18         // 是否遇到过数字
19         boolean seen = false;
20         for (int d : res) {
21             // skip the leading zeros
22             if (d == 0 && !seen) {
23                 continue;
24             }
25             sb.append(d);
26             seen = true;
27         }
28         if (sb.length() == 0) {
29             return "0";
30         }
31         return sb.toString();
32     }
33 }

 

LeetCode 题目总结

posted @ 2020-03-26 03:01  CNoodle  阅读(522)  评论(0)    收藏  举报