Product of Array Except Self
Problem statement
Given an integer array nums, return an array answer where answer[i] equals the product of every element of nums except nums[i].
Solve the problem without division in O(n) time. The output array does not count as extra space.
Function
productExceptSelf(nums: int[]) → int[]Examples
Example 1
nums = [1,2,3,4]return = [24,12,8,6]For index 0, the product is 2 * 3 * 4 = 24; apply the same rule at every index.
Example 2
nums = [-1,1,0,-3,3]return = [0,0,9,0,0]Only the position containing zero has a nonzero result, equal to (-1) * 1 * (-3) * 3 = 9.
Example 3
nums = [2,3]return = [3,2]With two values, each output is the other value.
Constraints
2 <= nums.length <= 10^5.-30 <= nums[i] <= 30.- Every prefix product and suffix product fits in a signed 32-bit integer.