打卡系列-剑指 Offer 66. 构建乘积数组

    科技2022-07-13  118

    给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B 中的元素 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。

    上三角和下三角

    public int[] constructArr1(int[] a) { int[] B = new int[a.length]; if(B.length <= 0){ return B; } int i; B[0] = 1; //下三角 for(i = 1 ; i < a.length ; i++){ B[i] = B[i - 1] * a[i - 1]; } //上三角 int tmp = 1; for(i = a.length - 2 ; i >= 0 ; i--){ tmp *= a[i + 1]; B[i] *= tmp; } return B; }

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    Processed: 0.014, SQL: 8