题解 | #构建乘积数组#
构建乘积数组
https://www.nowcoder.com/practice/94a4d381a68b47b7a8bed86f2975db46
前缀和思想,两遍遍历,第一遍计算前缀积,第二遍计算后缀积并与同一下标的前缀积相乘。
class Solution { public: vector<int> multiply(const vector<int>& A) { int n = A.size(); vector<int> res(A); for (int i = 1; i < n; i++) res[i] = res[i - 1] * A[i - 1]; int r = A.back(); for (int i = n - 2; i >= 0; i--) { res[i] *= r; r *= A[i]; } return res; } };