My short and simple C++ solution


  • 0
    H

    I add a dummy value in the first place to help.

    class NumArray {
    public:
        NumArray(vector<int> &nums) {
            array.push_back(0);
            for(int i = 0; i < nums.size(); i++){
                array.push_back(nums[i] + array[i]);
            }
        }
    
        int sumRange(int i, int j) {
            return array[j + 1] - array[i];
        }
    private:
        vector<int > array;
    };

Log in to reply
 

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