思路
使用二个dp数组分别维护i左侧的乘积和i右侧的乘积
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
int n = a.size();
vector<int> res(n), dpl(n, 1), dpr(n, 1);
for (int i = 1; i < n; ++i) {
dpl[i] = dpl[i-1] * a[i-1];
}
for (int i = n - 2; i >= 0; --i) {
dpr[i] = dpr[i+1] * a[i+1];
}
for (int i = 0; i < n; ++i) {
res[i] = dpl[i] * dpr[i];
}
return res;
}
};