前缀和的思想——前缀后缀积-
构建乘积数组
http://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46
class Solution { public: vector<int> multiply(const vector<int>& A) { // 定义前缀积f[i]表示前i个数的乘积,后缀积为第i到最后一个元素的乘积 vector<int> f1, f2; f1.push_back(1); f2.push_back(1); vector<int> b; for(int i = 1; i <= A.size(); i ++) { f1.push_back(f1[i - 1] * A[i - 1]); f2.push_back(f2[i - 1] * A[A.size() - i]); } for(int i = 1; i <= A.size(); i ++) { b.push_back(f1[i - 1] * f2[A.size() - i]); } return b; } };