思路:左右数组
左边乘积数组和右边乘积数组,手动计算头尾的值,最后自动迭代
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
//[1,2,6,24]
//[24,24,12,4]
int n = nums.size();
vector<int> l_multi(n,0);
vector<int> r_multi(n,0);
vector<int> res(n,0);
int tem = 1;
for(int i = 0;i<l_multi.size();i++){
tem *= nums[i];
l_multi[i] = tem;
}
tem = 1;
for(int i = r_multi.size()-1;i>=0;i--){
tem *= nums[i];
r_multi[i] = tem;
}
res[0] = r_multi[1];
res[n-1] = l_multi[n-2];
for(int i=1;i<n-1;i++){
res[i] = l_multi[i-1]*r_multi[i+1];
}
return res;
}
};