/* * @lc app=leetcode id=238 lang=javascript * * [238] Product of Array Except Self * * https://leetcode.com/problems/product-of-array-except-self/description/ * * algorithms * Medium (53.97%) * Total Accepted: 246.5K * Total Submissions: 451.4K * Testcase Example: '[1,2,3,4]' * * 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] * * * Note: Please solve it without division and in O(n). * * Follow up: * Could you solve it with constant space complexity? (The output array does * not count as extra space for the purpose of space complexity analysis.) * */ /** * @param {number[]} nums * @return {number[]} */ var productExceptSelf = function(nums) { const ret = []; for (let i = 0, temp = 1; i < nums.length; i++) { ret[i] = temp; temp *= nums[i]; } // 此时ret[i]存放的是前i个元素相乘的结果(不包含第i个) // 如果没有上面的循环的话, // ret经过下面的循环会变成ret[i]存放的是后i个元素相乘的结果(不包含第i个) // 我们的目标是ret[i]存放的所有数字相乘的结果(不包含第i个) // 因此我们只需要对于上述的循环产生的ret[i]基础上运算即可 for (let i = nums.length - 1, temp = 1; i >= 0; i--) { ret[i] *= temp; temp *= nums[i]; } return ret; };