[LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices iand 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.
这道题让我们检索一个数组的某个区间的所有数字之和,题目中给了两条条件,首先数组内容不会变化,其次有很多的区间和检索。那么我们用传统的遍历相加来求每次区间和检索,十分的不高效,而且无法通过 OJ。所以这道题的难点就在于是否能想到来用建立累计直方图的思想来建立一个累计和的数组 dp,其中 dp[i] 表示 [0, i] 区间的数字之和,那么 [i,j] 就可以表示为 dp[j]-dp[i-1],这里要注意一下当 i=0 时,直接返回 dp[j] 即可,参见代码如下:
解法一:
class NumArray {
public:
NumArray(vector<int> &nums) {
dp = nums;
for (int i = ; i < nums.size(); ++i) {
dp[i] += dp[i - ];
}
}
int sumRange(int i, int j) {
return i == ? dp[j] : dp[j] - dp[i - ];
}
private:
vector<int> dp;
};
当然,我们也可以通过增加一位 dp 的长度,来避免在 sumRange 中检测i是否为0,参见代码如下:
解法二:
class NumArray {
public:
NumArray(vector<int> &nums) {
dp.resize(nums.size() + , );
for (int i = ; i <= nums.size(); ++i) {
dp[i] = dp[i - ] + nums[i - ];
}
}
int sumRange(int i, int j) {
return dp[j + ] - dp[i];
} private:
vector<int> dp;
};
类似题目:
Range Sum Query 2D - Immutable
Maximum Size Subarray Sum Equals k
参考资料:
https://leetcode.com/problems/range-sum-query-immutable/
https://leetcode.com/problems/range-sum-query-immutable/discuss/75184/5-lines-C%2B%2B-4-lines-Python
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变的更多相关文章
- [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 ...
- 303 Range Sum Query - Immutable 区域和检索 - 不可变
给定一个数组,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点.例如:给定nums = [-2, 0, 3, -5, 2, -1],求和函数为sumRange() ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable
The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...
- LeetCode——Range Sum Query - Immutable
Question Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- [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 ...
- [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 ...
随机推荐
- C#索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- Google地图开发总结
我们经常使用地图查位置.看公交.看街景,同时地图还开放第三方的API给开发者.利用这些API进行地图的个性化的展示和控制,例如北京被水淹了,开发一个网页显示北京被淹的地图,地图上面标志被水淹的位置.严 ...
- 学习javascript数据结构(三)——集合
前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...
- .net程序部署(setupFactory进阶)
接上一篇 继续使用上一篇的project .将archive里无用的文件删除 添加我们需要的文件进来. config是一个文本文件. 注意所有文件的 destination都是 %appfolder% ...
- enote笔记语言(2)
why not(whyn't) 为什么不(与“why”相对应,是它的反面) how对策 how设计 key-memo ...
- java单例模式的实现方式
一.什么是单例模式 单例:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的软件设计模式之一,其目的是保证整个应用中只存在类的唯一个实例. 比如我们在系统启动时,需要加载一些 ...
- 记录一次bug解决过程:git深入学习和JDK8新特性
一 总结 熟悉廖雪峰git基础; 由于git跟踪的是修改,而不是版本号:因此对于修改撤销的操作,文件在eclipse中依旧有>修改标记,这点不同于svn. 二 BUG描述:熟悉Git基础 在Gi ...
- 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型
在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...
- 《连载 | 物联网框架ServerSuperIO教程》-4.如开发一套设备驱动,同时支持串口和网络通讯。附:将来支持Windows 10 IOT
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- UITableView点击每个Cell,Cell的子内容的收放
关于点击TableviewCell的子内容收放问题,拿到它的第一个思路就是, 方法一: 运用UITableview本身的代理来处理相应的展开收起: 1.代理:- (void)tableView:(UI ...