303. Range Sum Query - Immutable
题目:
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.
链接: 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相关推荐
- [LeetCode] 303. Range Sum Query - Immutable
https://leetcode.com/problems/range-sum-query-immutable/ 用一个 sum 数组,sum[i] -- nums 中选出前 i 个元素,求和所得到的 ...
- leetcode 303. Range Sum Query - Immutable | 303. 区域和检索 - 数组不可变(一维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-immutable/ 题解 标准的前缀和问题,简单题,不多说,直接上代码 import java.ut ...
- 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 ...
- Leetcode题目:Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- 重拾编程之路--jeetcode(java)--Range Sum Query - Immutable
算法理解: 1)用一个和数组记录前几个元素的和: sum[0]---(0,0)的和: sum[1]-- (0,1)的和: ...
- LeetCode Range Sum Query Immutable
2131231 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10873807.html
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- 304. Range Sum Query 2D - Immutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
最新文章
- awk 6.0 — awk模式之二
- GIF Movie Gear V4.2.3 破解版
- B - Bone Collector (01背包)
- linux nat软件,linux下nat的应用(转)
- DeeplabV3+ 在自己环境下跑出现的错误
- yii2史上最简单式安装教程,没有之一
- java实现抽奖游戏_Java实现游戏抽奖算法
- JAVA做一个五星评论打分字体,java中的Font
- c语言找出递增子数组的长度,编程之美2.16 数组中最长递增子序列的长度
- 计算机演示文稿教学实例课件,计算机图形学的教学演示文稿ppt课件.ppt
- 问题 G: Search Problem (IV)
- pip install numpy 安装numpy失败
- 眼睛容易干燥疲劳怎么办?
- MSP、CSP、BSP区别
- baseline的骨骼检测流程记录
- 举例说明 频分多址FDMA、时分多址TDMA、码分多址CDMA、空分多址SDMA的异同
- android 模拟器 派派,派派怎么在电脑上玩?派派电脑版使用教程
- Vulhub靶场的搭建(下载和安装)
- 华为究竟做了多少芯片?
- 下沉市场“四大天王”受关注 水滴筹成健康保障领域独角兽