/** * C++ * Your NumArray object will be instantiated and called as such: * NumArray* obj = new NumArray(nums); * int param_1 = obj->sumRange(i,j); */ classNumArray { public: NumArray(vector<int>& nums) { int n = nums.size(); if (n==0) return; sum_ = vector<int> (n, 0); sum_[0] = nums[0]; for (int i=1;i<nums.size();i++){ sum_[i] = sum_[i-1] + nums[i]; } }
intsumRange(int i, int j){ if(i==0) return sum_[j]; return sum_[j] - sum_[i-1]; } private: vector<int> sum_; };