利用一个(n+1)的数组然后不断的更新前i项的和放在i+1位置中
class NumArray {
public:
vector<int>sum;
NumArray(vector<int>& nums)
{
sum.resize(nums.size()+1,0);
for(int i=0;i<nums.size();i++)
{
sum[i+1]=sum[i]+nums[i];
}
}
int sumRange(int i, int j)
{
return sum[j+1]-sum[i];
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* int param_1 = obj->sumRange(i,j);
*/