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);
 */

leetcode 303. 区域和检索 - 数组不可变

上一篇:303.区域和检测-数组不可变


下一篇:leetcode——区域和检索