# LeetCode 303 区域和检索 HERODING的LeetCode之路

NumArray(int[] nums) 使用数组 nums 初始化对象
int sumRange(int i, int j) 返回数组 nums 从索引 i 到 j（i ≤ j）范围内元素的总和，包含 i、j 两点（也就是 sum(nums[i], nums[i + 1], ... , nums[j])）


[“NumArray”, “sumRange”, “sumRange”, “sumRange”]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]

[null, 1, -1, -3]

NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))

0 <= nums.length <= 104
-105 <= nums[i] <= 105
0 <= i <= j < nums.length



class NumArray {
private:
vector<int> sum;
public:
NumArray(vector<int>& nums) {
if(nums.size() == 0) {
return;
}
sum.push_back(nums[0]);
for(int i = 1; i < nums.size(); i ++) {
sum.push_back(nums[i] + sum[i - 1]);
}
}

int sumRange(int i, int j) {
if(i == 0) {
return sum[j];
} else {
return sum[j] - sum[i - 1];
}
}
};

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

/*作者：heroding



11-28 1044
02-04 301
05-28 9431
11-13 6779
12-06 23
09-18 2891
03-02 2
04-06