-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path303.range-sum-query-immutable.cpp
More file actions
54 lines (52 loc) · 1.23 KB
/
303.range-sum-query-immutable.cpp
File metadata and controls
54 lines (52 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode id=303 lang=cpp
*
* [303] Range Sum Query - Immutable
*
* https://leetcode.com/problems/range-sum-query-immutable/description/
*
* algorithms
* Easy (38.58%)
* Total Accepted: 146.4K
* Total Submissions: 376.4K
* Testcase Example: '["NumArray","sumRange","sumRange","sumRange"]\n[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]'
*
* 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:
*
* You may assume that the array does not change.
* There are many calls to sumRange function.
*
*
*/
class NumArray {
public:
NumArray(vector<int>& nums) {
partSum = vector<int>(nums.size() + 1);
partSum[0] = 0;
for (int i = 1; i <= nums.size(); ++i)
partSum[i] = partSum[i-1] + nums[i-1];
return;
}
int sumRange(int i, int j) {
return partSum[j+1] - partSum[i];
}
private:
vector<int> partSum;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* int param_1 = obj->sumRange(i,j);
*/