var productExceptSelf = function(nums) {
const products = new Array(nums.length).fill(1);
for (let i = 1; i < nums.length; i++) {
products[i] = nums[i - 1] * products[i - 1];
}
let acc = 1;
for (let i = nums.length - 1; i >= 0; i--) {
products[i] = products[i] * acc;
acc = acc * nums[i];
}
return products;
};
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
const left = new Array(nums.length).fill(1);
const right = new Array(nums.length).fill(1);
// 1 1 2 6
// 24 12 4 1
for (let i = 1; i < nums.length; i++) {
left[i] = nums[i - 1] * left[i - 1];
}
for (let i = nums.length - 2; i >= 0; i--) {
right[i] = nums[i + 1] * right[i + 1];
}
const products = [];
for (let i = 0; i < nums.length; i++) {
products[i] = left[i] * right[i];
}
return products;
};