Here is the O(N) based C++ implementation

```
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int len=nums.size();
vector<int> left(len, 1);
vector<int> right(len, 1);
vector<int> result(len, 0);
for(int i=1; i<len; i++) left[i]=left[i-1]*nums[i-1];
for(int i=len-2; i>=0; i--) right[i]=right[i+1]*nums[i+1];
for(int i=0; i<len; i++) result[i]=left[i]*right[i];
return result;
}
};
```

How to use O(1) ?

By observing the above code, we can just for every position multiply it to its right position.

Just the idea to think reversly !

```
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
int left=1, right=1;
vector<int> result(n, 1);
for(int i=0; i<n; i++){
result[i]*=left;
result[n-1-i]*=right;
left*=nums[i];
right*=nums[n-1-i];
}
return result;
}
};
```