Skip to content

Commit b5e773a

Browse files
committed
product-of-array-except-self solution
1 parent ea6e64b commit b5e773a

File tree

1 file changed

+21
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)