Skip to content

Commit f307f6c

Browse files
committed
📝 Docs: solved 1 - productExceptSelf
1 parent 8937e39 commit f307f6c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 본인을 기준으로 좌측과, 우측 나눠서 곱한다.
3+
* 담아놓을 변수를 놓고 진행한다.
4+
* @param {number[]} nums
5+
* @return {number[]}
6+
*
7+
* 시간 복잡도: O(n)
8+
* 공간 복잡도: O(n)
9+
*/
10+
var productExceptSelf = function(nums) {
11+
const n = nums.length;
12+
const left = Array(n).fill(1);
13+
const right = Array(n).fill(1);
14+
const res = Array(n);
15+
16+
// 좌측 곱
17+
for (let i = 1; i < n; i++) {
18+
left[i] = nums[i - 1] * left[i - 1];
19+
}
20+
21+
// 우측 곱
22+
for (let i = n - 2; i >= 0; i--) {
23+
right[i] = nums[i + 1] * right[i + 1];
24+
}
25+
26+
for (let i = 0; i < n; i++) {
27+
res[i] = left[i] * right[i];
28+
}
29+
30+
return res;
31+
};

0 commit comments

Comments
 (0)