求区间,固定的数组,用前缀和数组
class NumArray {
public:
vector<int> prefix;
NumArray(vector<int>& nums) {
prefix.push_back(0);
for(int i=0;i<nums.size();i++)
{
prefix.push_back(prefix[i]+nums[i]);
}
}
int sumRange(int i, int j) {
return prefix[j+1]-prefix[i];
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* int param_1 = obj->sumRange(i,j);
*/