238. Product of Array Except Self

class Solution {
    public int[] productExceptSelf(int[] nums) {
        
        int n=nums.length;
        
        int [] fwd=new int[n];
        int [] bwd=new int[n];
        int [] res=new int[n];
        
        bwd[n-1]=1;
        fwd[0]=1;
        
        for(int i=1;i<n;i++)
        {
            fwd[i]=fwd[i-1]*nums[i-1];
        }
        
        for(int i=n-2;i>=0;i--)
        {
            bwd[i]=bwd[i+1]*nums[i+1];
        }
        
        for(int i=0;i<n;i++)
        {
            res[i]=fwd[i]*bwd[i];
        }
        
        return res;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章