Skip to content

Commit d45338c

Browse files
committed
product-of-array-except-self풀이
1 parent 87fc3f9 commit d45338c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function (nums) {
6+
const numsLength = nums.length;
7+
8+
const result = new Array(numsLength).fill(1);
9+
10+
let left = 1;
11+
let right = 1;
12+
13+
for (let i = 0; i < numsLength; i++) {
14+
result[i] *= left;
15+
left *= nums[i];
16+
17+
result[numsLength - i - 1] *= right;
18+
right *= nums[numsLength - i - 1];
19+
}
20+
21+
// TC: O(n), SC: O(1)
22+
23+
return result;
24+
};
25+
26+
console.log(productExceptSelf([1, 2, 3, 4]));

0 commit comments

Comments
 (0)