Clear. Easy to understand code.


  • 0
    O
    
        public int[] productExceptSelf(int[] nums) {
            int[] result = new int[nums.length];
            int size = nums.length;
            for(int i = 0; i < size; i++) {
                result[i] = 1;
            }
            for(int i = 1; i < size; i++) {
                result[i] = result[i-1]*nums[i-1];
            }
            int tmp = nums[size-1];
            for(int i = size - 2; i >= 0; i--) {
                result[i] *= tmp;
                tmp *= nums[i];
            }
            return result;
        }
    

Log in to reply
 

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