Skip to content

Commit ee2048b

Browse files
committed
Time: 5 ms (69.41%), Space: 55.8 MB (56.1%) - LeetHub
source:c220d50ed7142c1b5b11ee42d291f916a66247a4
1 parent e5d3bb4 commit ee2048b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} num1
3+
* @param {string} num2
4+
* @return {string}
5+
*/
6+
var multiply = function(num1, num2) {
7+
8+
if (num1 === "0" || num2 === "0") return "0";
9+
10+
const m = num1.length;
11+
const n = num2.length;
12+
13+
const res = new Array(m + n).fill(0);
14+
15+
for (let i = m - 1; i >= 0; i--) {
16+
for (let j = n - 1; j >= 0; j--) {
17+
const mul = num1[i] * num2[j];
18+
19+
const p1 = i + j; // 십의 자리
20+
const p2 = i + j + 1; // 일의 자리
21+
22+
const sum = mul + res[p2];
23+
24+
res[p2] = sum % 10;
25+
res[p1] += Math.floor(sum / 10);
26+
}
27+
}
28+
29+
30+
while (res[0] === 0) {
31+
res.shift();
32+
}
33+
34+
return res.join('');
35+
36+
};

0 commit comments

Comments
 (0)