剑指 Offer 66. 构建乘积数组(中等)

题目:剑指 Offer 66. 构建乘积数组(中等)_第1张图片剑指 Offer 66. 构建乘积数组(中等)_第2张图片

class Solution {
public:
    vector constructArr(vector& a) {
        int len = a.size();
        if(len==0) return {};
        vector b(len, 1);
        for(int i=1;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)

你可能感兴趣的:(剑指offer)