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);

题外话:

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

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

leetcode range sum query的更多相关文章

  1. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  2. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

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

  3. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

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

  5. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  6. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  8. [LeetCode] Range Sum Query 2D - Immutable

    Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...

  9. Leetcode: Range Sum Query - Mutable && Summary: Segment Tree

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

  10. [LeetCode] Range Sum Query - Immutable

    The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...

随机推荐

  1. Android 任何位置的可移动悬浮窗

    刚好要做这块的东西,所以网上翻了下资料,百度出来的基本上都是:默认起始位置左上角,还不能改动,一改动起始位置,第二次拖动就不正常了~~ 下面直接附上任意位置可拖动的源码(由于是demo写的比较乱): ...

  2. Jemeter基础

    jemeter主要组件: a.测试计划(Test Plan) 是使用JMeter进行测试的起点,它是其它JMeter测试元件的容器. b.线程组(Thread Group) 代表一定数量的并发用户,它 ...

  3. eclipse 修改中英文显示

    1.关闭eclipse 2.右键->选择属性--->打开文件位置 3.找到“eclipse.ini”文件,用EditPlus打开 4.在里面加 “-Duser.language=en”,即 ...

  4. 实现一个栈类,类似STL中的栈

    1.思路讲解 stack集合类是一个简单的堆栈的实现. 这里有两个模板参数,T和size,T用于指定堆栈中的元素类型,my_size用于表示堆栈中项数的最大值. 类中添加方法isempty.isful ...

  5. HDU 5527---Too Rich(贪心+搜索)

    题目链接 Problem Description You are a rich person, and you think your wallet is too heavy and full now. ...

  6. 使用Vue-resource完成交互

    使用vue-resource 引入vue-resource vue-resource就像jQuery里的$.ajax,是用来跟后端交互数据的,vue-resource是vue的一个插件,所以我们在开始 ...

  7. 移动端布局最佳实践(viewport+rem)

    通过前几天写的两篇博客(浅谈移动端三大viewport和移动端em和rem区别),我们现在来总结一下如何实现一个最佳方案. 之前在第二篇博客中提到过我们可以使用媒体查询来针对不同设备及做适配,如下图 ...

  8. CSS单位

    一.em单位:相对单位,相对于父级大小. <div class="fs"> 你看看我的字体大小 <div class="fs"> 你在看 ...

  9. NLP中的用N-gram语言模型做英语完型填空的环境搭建

    本文是对xing_NLP中的用N-gram语言模型做完型填空这样一个NLP项目环境搭建的一个说明,本来想写在README.md中.第一次用github中的wiki,想想尝试一下也不错,然而格式非常的混 ...

  10. UglyNumber - 找“丑数”

    uglynumber的定义是只能被1,2,3,5整除的数 规定1是第一个uglynumber:以此类推,1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 ...