Java concise solution


  • 0
    L
     public int[] productExceptSelf(int[] nums) {
            //For example, given [1,2,3,4], return [24,12,8,6].
            int[] output = new int[nums.length];
            int product=1;
            for(int i=nums.length-1;i>=0;i--) {
                output[i]=product;
                product *= nums[i];
            }   
            product = 1;
            for(int i=1;i<nums.length;i++) {
                product *= nums[i-1];
                output[i] *= product;
            }
            return output;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.