# 43. 字符串相乘
给定两个以字符串形式表示的非负整数
num1和num2,返回num1和num2的乘积,它们的乘积也表示为字符串形式。
输入:num1 = '2',num2='3'
输出:'6'
输入:num1='123', num2='456'
输出:`56088`
 1
2
3
4
5
2
3
4
5
# 思路
# 代码
const multiply = (num1, num2) => {
  const len1 = num1.length;
  const len2 = num2.length;
  const pos = new Array(len1 + len2).fill(0);
  for (let i = len1 - 1; i >= 0; i--) {
    const n1 = +num1[i];
    for (let j = len2 - 1; j >= 0; j--) {
      const n2 = +num2[j];
      const multi = n1 * n2;             
      const sum = pos[i + j + 1] + multi; 
      pos[i + j + 1] = sum % 10;
      pos[i + j] += sum / 10 | 0; // 取整
    }
  }
  while (pos[0] == 0) {
    pos.shift();
  }
  return pos.length ? pos.join('') : '0';
};
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
时间复杂度
O(n^2)