C# O(n) Runtime O(1) space


  • 0
    K

    Simply get the multiply factors to the left, and then the right.

    public class Solution {
        public int[] ProductExceptSelf(int[] nums) {
            int[] result = new int[nums.Length];
            for(int i = 0; i < result.Length; ++i){
                result[i] = 1;
            }
            
            for(int i = 1; i < result.Length; ++i){
                result[i] = result[i - 1] * nums[i - 1];
            }
            
            int right = nums[nums.Length - 1];
            
            for(int i = nums.Length - 2; 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.