题目:
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
int len = a.size();
if(len==0) return {};
vector<int> b(len, 1);
for(int i=1;i<len;i++){ //先计算b[i]下三角的乘积。由图可看出b[1]乘的是a[0]
b[i]=b[i-1]*a[i-1];
}
int tmp=1;
for(int i=len-2;i>=0;i--){ //再计算b[i]上三角的乘积,乘积用tmp存储和迭代。由图可看出b[3]乘的是a[4]
tmp*=a[i+1];
b[i]*=tmp;
}
return b;
}
};
作者:Krahets
链接:https://leetcode.cn/problems/gou-jian-cheng-ji-shu-zu-lcof/solutions/208840/mian-shi-ti-66-gou-jian-cheng-ji-shu-zu-biao-ge-fe/
来源:力扣(LeetCode)