题目:

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.

链接: http://leetcode.com/problems/range-sum-query-immutable/

3/7/2017

看别人答案的,之后就算有了思路还是做错。原因:没有仔细想清楚每个变量和数组的意义,比如start/end的元素是否包括,辅助数组的和是否包括当前值,以及辅助数组的长度。

 1 public class NumArray {
 2     int[] partialSum;
 3     public NumArray(int[] nums) {
 4         partialSum = new int[nums.length + 1];
 5         for(int i = 1; i < partialSum.length; i++) {
 6             partialSum[i] = nums[i-1] + partialSum[i-1];
 7         }
 8     }
 9
10     public int sumRange(int i, int j) {
11         return partialSum[j+1] - partialSum[i];
12     }
13 }
14
15
16 /**
17  * Your NumArray object will be instantiated and called as such:
18  * NumArray obj = new NumArray(nums);
19  * int param_1 = obj.sumRange(i,j);
20  */

转载于:https://www.cnblogs.com/panini/p/6517670.html

303. Range Sum Query - Immutable相关推荐

  1. [LeetCode] 303. Range Sum Query - Immutable

    https://leetcode.com/problems/range-sum-query-immutable/ 用一个 sum 数组,sum[i] -- nums 中选出前 i 个元素,求和所得到的 ...

  2. leetcode 303. Range Sum Query - Immutable | 303. 区域和检索 - 数组不可变(一维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-immutable/ 题解 标准的前缀和问题,简单题,不多说,直接上代码 import java.ut ...

  3. LeetCode 303. Range Sum Query - Immutable

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

  4. Leetcode题目:Range Sum Query - Immutable

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

  5. 重拾编程之路--jeetcode(java)--Range Sum Query - Immutable

    算法理解:         1)用一个和数组记录前几个元素的和:               sum[0]---(0,0)的和:               sum[1]-- (0,1)的和:     ...

  6. LeetCode Range Sum Query Immutable

    2131231 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10873807.html

  7. leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...

  8. 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...

  9. 304. Range Sum Query 2D - Immutable

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

最新文章

  1. awk 6.0 — awk模式之二
  2. GIF Movie Gear V4.2.3 破解版
  3. B - Bone Collector (01背包)
  4. linux nat软件,linux下nat的应用(转)
  5. DeeplabV3+ 在自己环境下跑出现的错误
  6. yii2史上最简单式安装教程,没有之一
  7. java实现抽奖游戏_Java实现游戏抽奖算法
  8. JAVA做一个五星评论打分字体,java中的Font
  9. c语言找出递增子数组的长度,编程之美2.16 数组中最长递增子序列的长度
  10. 计算机演示文稿教学实例课件,计算机图形学的教学演示文稿ppt课件.ppt
  11. 问题 G: Search Problem (IV)
  12. pip install numpy 安装numpy失败
  13. 眼睛容易干燥疲劳怎么办?
  14. MSP、CSP、BSP区别
  15. baseline的骨骼检测流程记录
  16. 举例说明 频分多址FDMA、时分多址TDMA、码分多址CDMA、空分多址SDMA的异同
  17. android 模拟器 派派,派派怎么在电脑上玩?派派电脑版使用教程
  18. Vulhub靶场的搭建(下载和安装)
  19. 华为究竟做了多少芯片?
  20. 下沉市场“四大天王”受关注 水滴筹成健康保障领域独角兽

热门文章

  1. BZOJ2876 [Noi2012]骑行川藏 【拉格朗日乘数法】
  2. 2017-12-04HTML布局_div布局
  3. 大话设计模式Python实现-观察者模式
  4. Spring整合Hibernate。。。。
  5. STM8启动分析及IAP
  6. Java基础学习(一)—方法
  7. Hibernate的懒加载问题
  8. Android开发(五)——计时器
  9. 5分钟在超能云(SuperVessel)上免费创建属于自己的大数据环境
  10. Tomcat中web.xml文件的详细说明