303. Range Sum Query – Immutable

https://leetcode.com/problems/range-sum-query-immutable/

class NumArray {
static int[] nums;
    public NumArray(int[] nums) {
     this.nums = nums;   
    }
    
    public int sumRange(int i, int j) {
        if (i == j)
            return nums[i];
        if (i +1 == j)
            return nums[i] + nums[j];
        
        return nums[i] + sumRange(i+1 , j-1) + nums[j];
        

    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s