Skip to content

Commit f121702

Browse files
committed
product-of-array-except-self
1 parent 724b9d5 commit f121702

File tree

1 file changed

+19
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)