leetcode 303. 区域和检索 - 数组不可变
作者:互联网
利用一个(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);
*/
标签:检索,obj,nums,NumArray,int,sum,303,sumRange,leetcode 来源: https://blog.csdn.net/qq_43180487/article/details/114270184