剑指 Offer 66. 构建乘积数组

public int[] constructArr(int[] a) {
        int len = a.length;
        if(len == 0) return new int[0];
        int[] b = new int[len];
        return multiply(a,b,len);
    }

    private int[] multiply(int[] a, int[] b,int len) {
        b[0] = 1;
        for(int i = 1;i<len;i++){
            b[i] = b[i-1]*a[i-1];
        }
        int temp = 1;
        for(int i = len-2;i>=0;i--){
            temp = temp * a[i+1];
            b[i] = b[i] * temp;
        }
        return b;
    }

 

posted @ 2020-08-22 09:59  欣姐姐  阅读(87)  评论(0编辑  收藏  举报