303. Range Sum Query - Immutable

class NumArray {
private:
    vector<int> v;
public:
    NumArray(vector<int> &nums) {
        int n = nums.size();
        v = vector<int>(n + 1);
        int sum = 0;
        for (int i = 1; i <= n; ++i) {
            sum += nums[i - 1];
            v[i] = sum;
        }
    }

    int sumRange(int i, int j) {
        return v[j + 1] - v[i];
    }
};
//596 ms

算法思路: sumRange(i, j) = sumRange(0, j) - sumRange(0, i - 1) (记sumRange(0, -1)=0).

时间复杂度: O(n).

空间复杂度: O(n).


C++ O(1) queries - just 2 extra lines of code

//@rantos22
class NumArray {
public:
    NumArray(vector<int> &nums) : psum(nums.size()+1, 0) {
        partial_sum( nums.begin(), nums.end(), psum.begin()+1);
    }

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

使用了partial_sum函数.