Skip to content

Commit 0770634

Browse files
committed
product-of-array-except-self solution
1 parent 363118e commit 0770634

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)