My C++ Solution using a extra sum array


  • 0
    H

    class NumArray {

    public:

    NumArray(vector<int> &nums) {
        for (int i = 0;i < nums.size();i++) {
            count += nums[i];
            sum.push_back(count);
        }
    }
    
    int sumRange(int i, int j) {
        if (i == 0) return sum[j];
        else return sum[j]-sum[i-1];
    }
    

    private:

    vector<int> sum;
    int count = 0;
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.