303. Range Sum Query - Immutable

Problem

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Note:
You may assume that the array does not change.
There are many calls to sumRange function.

Example

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class NumArray {
private:
    vector inner_nums;
public:
    NumArray(vector nums) {
        int len = nums.size();
        inner_nums.resize(len+1,0);
        for(int i=0;i

Result

303. Range Sum Query - Immutable.png

你可能感兴趣的:(303. Range Sum Query - Immutable)