Given an array of n integers where n > 1,
nums
, return an arrayoutput
such thatoutput[i]
is equal to the product of all the elements ofnums
exceptnums[i]
.Solve it without division and in O(n).
For example, given
[1,2,3,4]
, return[24,12,8,6]
.Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
主要就是预处理。把数组预先正序和逆序都累乘一遍,这样针对某个nums[i],只需要将其前面的正序积和其后面的逆序积相乘就可以得到答案了。
1 | // |