# [leetcode题解]303. Range Sum Query – Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

```Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
```

Note:

1. You may assume that the array does not change.
1. There are many calls to sumRange function.

```typedef struct {
int *nums;
int numsSize;
} NumArray;

NumArray* numArrayCreate(int* nums, int numsSize) {
int i = 0;

NumArray *array = calloc(1, sizeof(NumArray));
array->nums = calloc(numsSize, sizeof(int));
array->numsSize = numsSize;

memcpy(array->nums, nums, numsSize * sizeof(int));
for (i = 1; i < numsSize; i++) {
array->nums[i] += array->nums[i - 1];
}

return array;
}

int numArraySumRange(NumArray* obj, int i, int j) {
if (i < 0)
i = 0;
if (j > obj->numsSize - 1)
j = obj->numsSize - 1;

if (i == 0)
return obj->nums[j];
else
return (obj->nums[j] - obj->nums[i - 1]);
}

void numArrayFree(NumArray* obj) {
free(obj->nums);
free(obj);
}

/**
* Your NumArray struct will be instantiated and called as such:
* struct NumArray* obj = numArrayCreate(nums, numsSize);
* int param_1 = numArraySumRange(obj, i, j);
* numArrayFree(obj);
*/```