My java solution ( not sure whether it is constant space complexity)


  • 0
    X
    public class Solution {
        public int[] productExceptSelf(int[] nums) {
            if(nums == null || nums.length == 0){
                return null;
            }
            
            int length = nums.length;
            
            int[] out1 = new int[length];
    
            int temp = 1;
            for(int i=0; i<length; i++){
                
                out1[i] = temp;
                temp = temp*nums[i];
            }
            
            int [] output = new int[length];
            
            int temp2 = 1;
            for(int i=length-1; i>=0; i--){
    
                output[i] = out1[i]*temp2;
                temp2 = temp2*nums[i];
            }
            
    
            
            return output;
            
        }
    }

Log in to reply
 

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