C# single pass


  • 0
    H
    public int[] ProductExceptSelf(int[] nums) {
            int[] output = new int [nums.Length];
            int m1 = 1, m2 = 1;
            for(int i = 0; i < nums.Length; i++) output[i] = 1;
            
            for(int i = 0; i < nums.Length; i++) {
                output[i] *= m1;
                output[nums.Length - i - 1] *= m2;
                
                m1 *= nums[i];
                m2 *= nums[nums.Length - i - 1];
            }
            
            return output;
        }
    

Log in to reply
 

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