303

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define MAXN 10005

class NumArray {
public:
int prefix[MAXN] = {};

NumArray(vector<int>& nums) {
prefix[0] = 0;
for(int i = 1; i<=nums.size(); ++i){
prefix[i] = nums[i-1] + prefix[i-1];
}
}

int sumRange(int left, int right) {
return prefix[right+1] - prefix[left];
}
};

/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* int param_1 = obj->sumRange(left,right);
*/