Question

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

  • Example:

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

sumRange(0, 2) -> 1

sumRange(2, 5) -> -1

sumRange(0, 5) -> -3

  • Note:

    You may assume that the array does not change.

    There are many calls to sumRange function.

Solution

简单的动态规划,依次累加,table[j] - table[i - 1]就表示两者之间的求和,注意i - 1可能越界。

Code

class NumArray {
public:
NumArray(vector<int> nums) {
int total = 0;
for (int i = 0; i < nums.size(); i++) {
total += nums[i];
table.push_back(total);
}
}
int sumRange(int i, int j) {
return (table[j] - ((i - 1) < 0 ? 0 : table[i - 1]));
}
private:
vector<int> table;
}; /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/

LeetCode——Range Sum Query - Immutable的更多相关文章

  1. [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 ...

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

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

  3. [LeetCode] Range Sum Query - Immutable

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

  4. [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 ...

  5. [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 ...

  6. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  7. [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 ...

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

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

  9. LeetCode_303. Range Sum Query - Immutable

    303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...

随机推荐

  1. c#修改cpu主频

    并不是真正能修改硬件,只是一个数据,能骗过部分程序检测,如英雄联盟必须达到3.0的主频才能使用录像功能,通过修改可以达到要求. 下面是代码: public enum RegValueKind { // ...

  2. PHP webservice 的初接触

    webservice 现在是开始流行了,很多业务开启了这个功能.去年接触的一个金融类的项目有类似功能调用.当时没放心思研究,最两天试着接触了下,还真不错的.起步其实挺简单. 服务端的代码 server ...

  3. PHP & “Data” URL scheme(转)

    基本上所有的对文件操作的API, 都迁移到的了PHP stream上, 所以, 绝大部分对文件操作的API都是支持Data URL的. 当某个API需要操作对象是文件的时候, 我们其实是可以采用Dat ...

  4. CListCtrl控件使用方法总结

    今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtr ...

  5. odoo学习记录1

    1. odoo通过ORM(对象关系映射)实现底层数据与上层逻辑到关联,保证数据存储的安全性和使用上到便利性. 2. odoo由模块组成,每个模块包含:Bussiness Object, Data, W ...

  6. HBase存储架构

    以下的介绍是基于Apache Hbase 0.94版本: 从HBase的架构图上可以看出,HBase中的存储包括HMaster.HRegionServer.HRegion.Store.MemStore ...

  7. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  8. sql server 复制表从一个数据库到另一个数据库

    sql server 复制表从一个数据库到另一个数据库 /*不同服务器数据库之间的数据操作*/ --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQL ...

  9. sidekiq安装及使用

    参考:https://github.com/mperham/sidekiq/wiki/Getting-Started https://wdxtub.com/2016/07/06/sidekiq-gui ...

  10. qqwry.dat输出乱码问题及maven打包后资源文件大小不一致的问题

    使用qqwry.dat进行IP地理位置查询时,遇到一个问题即在本地测试时查询纯真库时正常,没有任何问题,但是打包传到服务器上便出现了乱码问题. 1.首先排除服务器的字符集编码的影响 使用如下命令验证了 ...