Java O(n) no extra space


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

  • 0
    M

    "no extra space"? I see right and i.


Log in to reply
 

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