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].

Example:

Input:
Output:
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).

``````class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int elem_size = nums.size();
vector<int> res(elem_size);
vector<int> left(elem_size);
left[0]=1;
vector<int> right(elem_size);
right[0]=1;

for(int i = 1;i < elem_size;i++)
{
left[i] = nums[i - 1]*left[i-1];
right[i] = nums[elem_size - i] * right[i -1];
}

for(int i = 0;i < elem_size;i++)
{
res[i] = left[i] * right[elem_size - 1 -i];
}

return res;
}
};``````

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

for(int i = 1;i < N;i++)
{
res[i] = res[i - 1] * nums[i - 1];
}
int right_start = 1;
for(int i = N - 1;i >= 0;i--)
{
res[i] = res[i] * right_start;
right_start = right_start * nums[i];
}

return res;
}
};``````

``````Runtime Error
Line 924: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:9``````

``````//正确形式：
int elem_size = nums.size();
vector<int> res(elem_size);
vector<int> left(elem_size);
//错误形式：
int elem_size = nums.size();
vector<int> res;
vector<int> left;``````