Loading

剑指Offer66.构建乘积数组

题目:https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/

思路:从左往右遍历一遍,再从左往右遍历。

代码:

class Solution {
    public int[] constructArr(int[] a) {
        int[] b = new int[a.length];
        for(int i=0, product = 1; i<a.length; product*=a[i++]){
            b[i] = product;
        }
        for(int i=a.length-1, product = 1; i>=0; product*=a[i--]){
            b[i] *= product;
        }
        return b;
    }
}

 

posted @ 2020-12-03 13:37  yoyuLiu  阅读(42)  评论(0编辑  收藏  举报