Description
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.
2.There are many calls to sumRange function.
Solution 1(C++)
class NumArray {
public:
vector<int> memo = {0};
NumArray(vector<int> nums) {
int summ = 0;
for (int n : nums) {
summ += n;
memo.push_back(summ);
}
}
int sumRange(int i, int j) {
return memo[j+1] - memo[i];
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
算法分析
目前还不太清楚这道题是想干嘛,所以给了个差评。
程序分析
略。