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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var multiply = function(num1, num2) {
debugger
let numa = num1.split("")
let numb = num2.split("")

let result = []
let re = []
if(numa.length === 0 || numb.length === 0) return '0'
if((numa.length === 1 && numa[0] === '0' )|| (numb.length === 1 && numb[0] === '0')) return '0'
result.length = (numa.length + numb.length)
result.fill(0)
for(let i = numa.length - 1; i >= 0; i--) {
for(let j = numb.length -1; j >= 0; j--) {
let sum = numa[i] * numb[j]
let p1 = i + j
let p2 = i + j + 1
sum += result[p2]
result[p1] += sum / 10 >> 0
result[p2] = sum % 10
}
}

let k = 0
while(result[k] === 0) k++

for(let i = k; i < result.length; i++) {
re.push(result[i])
}
return re.join("")
};

题目连接
参考资料

感觉不错的话给博主赞助一下呗