No.44-LeetCode303 前缀和

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

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray* obj = new NumArray(nums);
 * int param_1 = obj->sumRange(i,j);
 */

你可能感兴趣的:(leetcode)