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].
Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
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.)
給予有n個整數的陣列nums且n > 1,回傳一個陣列output且output[i]等於nums所有元素的乘積除了nums[i]。
制約:保證乘積一定在32位元的表示範圍之內。
注意:請不使用除法並且在O(n)的時間複雜度之下完成此題。
進階:
你可以在常數等級的空間複雜度完成這題嗎?(output陣列並不算在額外空間之中。)
自己前面的乘積
* 自己後面的乘積
。O(3n) = O(n)
。back
和front
其實只需要其中一個,另一個的運算可以直接跟原本的nums
結合就好。
back
。LeetCode
C++