Product of Array Except Self

Given an arraynumsofn_integers where_n> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[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.)

Note

使用左右遍历的方法,记录left乘以right

left表示这数字左边所有数的乘积 -- 这里直接写入res数组

right表示这数字右边所有数的乘积 -- 这里使用常数迭代更新

Numbers: 2     3    4    5
Lefts:   1     2    2*3  2*3*4
Rights:  3*4*5 4*5  5    1
------------------------------
Result:  60    40   30  24

Code

Last updated