[JAVA] Two loops / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        public int[] productExceptSelf(int[] nums) {
            int[] result = new int[nums.length];
            int left = 1;
            for(int i = 0; i < nums.length; i++){
                result[i] = left;
                left *= nums[i];
            }
            int right = 1;
            for(int i = nums.length-1; i >= 0 ; i--){
                result[i] *= right;
                right *= nums[i];
            }
            return result;
        }
    }
    

Log in to reply
 

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