[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 ...
随机推荐
- SQL Server 统计信息更新时采样百分比对数据预估准确性的影响
为什么要写统计信息 最近看到园子里有人写统计信息,楼主也来凑热闹. 话说经常做数据库的,尤其是做开发的或者优化的,统计信息造成的性能问题应该说是司空见惯. 当然解决办法也并非一成不变,“一招鲜吃遍天” ...
- 卷积神经网络提取特征并用于SVM
模式识别课程的一次作业.其目标是对UCI的手写数字数据集进行识别,样本数量大约是1600个.图片大小为16x16.要求必须使用SVM作为二分类的分类器. 本文重点是如何使用卷积神经网络(CNN)来提取 ...
- 《高性能javascript》 领悟随笔之-------DOM编程篇
<高性能javascript> 领悟随笔之-------DOM编程篇一 序:在javaSctipt中,ECMASCRIPT规定了它的语法,BOM实现了页面与浏览器的交互,而DOM则承载着整 ...
- 为Guid数据类型的属性(property)赋值
先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.
- SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
相信很多人进行数据存储时,会遇上如标题的异常错误. 其实也不算上一个错误. 当你的程序中有宣告一个字段的数据类型为DateTime时,但你又没有赋值给它,就进行存储时,它就会得到这样一个结果. 看看下 ...
- Ubuntu下的解压缩
一. 命令: .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz ...
- 配置mac百度云同步盘
1. 选择同步盘在电脑中的位置,该文件夹中的内容与云端保持一致.默认位置/Users/LemonVerbena/百度云同步盘.电脑同步盘的作用与百度云网盘的主页一样,下面可以包括多个同步文件夹. 2. ...
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- Atitit.软件架构高扩展性and兼容性原理与概论实践attilax总结
Atitit.软件架构高扩展性and兼容性原理与概论实践attilax总结 1. 什么是可扩展的应用程序?1 2. 松耦合(ioc)2 3. 接口的思考 2 4. 单一用途&模块化,小粒度化2 ...
- 炫酷的jQuery对话框插gDialog
js有alert,prompt和confirm对话框,不过不是很美体验也不是很好,用jQuery也能实现, 体验效果:http://hovertree.com/texiao/jquery/34/ 代码 ...