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