构建乘积数组(三角阵形式循环乘)
构建乘积数组
http://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46
class Solution { public: vector<int> multiply(const vector<int>& A) { vector<int> B(A.size(), 1); // 声明一个B数组 for (int i=1; i<A.size(); ++i) { B[i] = B[i-1] * A[i-1]; // B[i] = a[0]*..a[i-1] } int tmp = 1; for (int j=A.size()-2; j>=0; --j) { tmp *= A[j+1]; // H[i] = a[i+1]*a[i+3]..a[n-1]; 用一个变量tmp *= a[j+1] ,j从后往前 B[j] *= tmp; } return B; } };