Product of Array Except Self
Given an arraynums
ofn_integers where_n> 1, return an arrayoutput
such thatoutput[i]
is equal to the product of all the elements ofnums
exceptnums[i]
.
Example
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表示这数字右边所有数的乘积 -- 这里使用常数迭代更新
Code
Last updated