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:

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

Solution 1:

class NumArray {

    int[] prefix;
public NumArray(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
prefix = new int[nums.length];
prefix[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
prefix[i] = prefix[i - 1] + nums[i];
}
} public int sumRange(int i, int j) {
if (i == 0) {
return prefix[j];
}
return prefix[j] - prefix[i - 1];
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/

Solution 2:

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

[LC] 303. Range Sum Query - Immutable的更多相关文章

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

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

  2. [LeetCode] 303. 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】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  4. 【leetcode❤python】 303. Range Sum Query - Immutable

    #-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...

  5. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  6. 303. Range Sum Query - Immutable

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

  7. Java [Leetcode 303]Range Sum Query - Immutable

    题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  8. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  9. 303. Range Sum Query - Immutable(动态规划)

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

随机推荐

  1. CAR-T|Single cell plan|Extracellular RNA|

    生物医疗大数据 安吉丽娜朱莉发现抑癌基因事件,BRCA突变与乳腺癌关联. 个体化测序商品23 and me 多组学数据研究:eg:太空和地球双胞胎发现生化指标差不多. 研究模式和工业模式相结合. 研究 ...

  2. Linux--Centos7开启关闭端口

    参考 http://blog.csdn.net/u013410747/article/details/61696178 查看已开放的端口(默认不开放任何端口)    firewall-cmd --li ...

  3. JavaScript sort()方法总结

    sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 注:如果调用该方法时没有使用参数,将按字母顺序对 ...

  4. Animate.css 一款强大的预设css3动画库

    Animate.css是一个有趣的,跨浏览器的css3动画库.很值得我们在项目中引用. 用法 1.首先引入animate css文件 <head> <link rel="s ...

  5. 三阶平面魔方(BFS)

    有一个  3×3 的平面魔方,在平面魔方中,每个格子里分别无重复地写上 1 - 9 这 9 个数字.一共有 4 种对平面魔方的操作: 选择某一行左移. 选择某一行右移. 选择某一列上移. 选择某一列下 ...

  6. windows 安装Bitcoin Core使用

    1.官网下载https://bitcoin.org/en/download 选择Windows  其他系统就选择对应的就好 2.双击安装完过后,进入bin目录,打开bitcoin-qt.exe运行,提 ...

  7. docker---设置镜像加速器

    国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器,国内很多云服务商都提供了国内加速器服务,如: Azure 中国镜像: https://dockerhub.azk8s.cn ...

  8. 你需要了解的JIT Debugging

    原总结debug调试dump转储文件windbgprocdumpJIT Debugger 如果你还不清楚什么是转储文件,不知道什么时候需要转储文件,请参考转储文件系列文章的第一篇 -- 转储文件知多少 ...

  9. ZJNU 2201 - 挖矿谷物语

    在dfs过程中加上栈记录当次dfs走过的路径 如果当次dfs到了一个之前的dfs已经经过的点 又因为只对没有访问过的点开始dfs 所以这种情况就说明接下来不可能返回到当次dfs开始的点 将栈内元素取出 ...

  10. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...