C# - pre-compute sums in new array with + 1 length


  • 0
    private int[] sums = null;
    public NumArray(int[] nums) {
        this.sums = new int[nums.Length + 1];
        for (int i = nums.Length - 1; i >= 0; i--)
        {
            this.sums[i] = this.sums[i+1] + nums[i];
        }
    }
    
    public int SumRange(int i, int j) {
        return this.sums[i] - this.sums[j+1];
    }

Log in to reply
 

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