leetcode range sum query

Given an integer array nums, find the sum of the elements between indices i and j (ij), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

刚开始看题,有些不懂,暴力破解后time exceed。很简单,聪明的人大概一会儿就想出来了。

 class NumArray {
vector<int> sumArray;
public:
NumArray(vector<int> &nums) { int temp=;
for(int i=;i<nums.size();i++){
temp+=nums[i];
sumArray.push_back(temp);
}
} int sumRange(int i, int j) {
int result;
result=this->sumArray[j]-this->sumArray[i-];
return result;
}
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

题外话:

最近状态有些不好,我事实上是一个常常状态不好的人,我【容易受环境干扰】、【理性感性参半】、【不容易坚守自己】

因为上述的缺点,常常陷入责怪自己的状态中,却不知道如何改变,最差却一直使用的解决办法就是明天再说,也许明天就好了。呸。

上一篇:MySQL中format()函数


下一篇:每日英语:Mistrust Between U.S., Malaysia Strains Probe