参考:https://leetcode.com/problems/product-of-array-except-self/discuss/65622/Simple-Java-solution-in-O(n)-without-extra-space



class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size(),1);
int right = 1;

for(int i=1;i<nums.size();i++)
{
res[i] = res[i-1] * nums[i-1];
}

for(int i=nums.size()-1;i>=0;i--)
{
res[i] *= right;
right *= nums[i];
}
return res;
}
};


238. Product of Array Except Self除自身以外数组的乘积_html