Question:
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]Output: [24,12,8,6]
Solution:
/*** @param {number[]} nums* @return {number[]}*/var productExceptSelf = function(nums) {const len = nums.length;const result = new Array(len);result.fill(1);// 计算左边for (let i = 1; i < len; i++) {result[i] = result[i-1] * nums[i-1];}// 右边for (let j = len - 2; j >= 0; j--) {result[j] *= nums[j+1];nums[j] *= nums[j+1];}return result;};
Runtime: 88 ms, faster than 78.80% of JavaScript online submissions for Product of Array Except Self.
