Leetcode之Range Sum Query - Immutable/Mutable

303. Range Sum Query - Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

这道题让我们检索一个数组的某个区间的所有数字之和,题目中给了两条条件,首先数组内容不会变化,其次有很多的区间和检索。那么我们用传统的遍历相加来求每次区间和检索,十分的不高效,而且无法通过OJ。
所以这道题的难点就在于是否能想到来用建立累计直方图的思想来建立一个累计和的数组dp,首先通过增加一位dp的长度,来避免在sumRange中检测i是否为0,其中dp[i]表示[0, i-1]区间的数字之和,那么[i,j]就可以表示为dp[j+1]-dp[i]。

class NumArray {
public:
    NumArray(vector<int> nums) {
        dp.resize(nums.size()+1,0);
        for(int i=1;i<=nums.size();i++)
            dp[i]=dp[i-1]+nums[i-1];
    }

    int sumRange(int i, int j) {
        return dp[j+1]-dp[i];
    }
private:
    vector<int> dp;
};

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

猜你喜欢

转载自blog.csdn.net/qq_21815981/article/details/80157063
今日推荐