Simple C++ solution


  • 0
    D
    class NumArray {
        vector<int> given;
    public:
        NumArray(vector<int> &nums) {
             if(nums.empty()) {
                given.push_back(0);
             }else{
                 given = nums;
                 given[0] = nums[0];
                 int length = nums.size();
                 for(int i = 1; i < length; ++i){
                    given[i] = given[i - 1] + nums[i];
                 }
             }
        }
    
        int sumRange(int i, int j) {
            return given[j] - given[i - 1];
        }
    };

Log in to reply
 

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