[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 ...
随机推荐
- 计算机网络学习笔记--网络层之IP地址与子网
IPv4地址: 我们知道在网络层(TCP/IP体系结构的网际互联层),最重要的一个协议就是IP协议,现在正处于IPv4和IPv6的过渡时期,但目前来说,IPv4仍为主流,所以主要讲Ipv4. IP地址 ...
- Android面试经验 -- 乐视
此次投的是三年经验的Android开发,最后反而因为自己的失误,没有准备充分而导致结果很悲剧,以此告诫自己千万不能疏忽大意. 面试过程 第一次去大公司面试,心里不是一般的激动和紧张,来到乐视大厦门口, ...
- 一个简单的webservice的demo(下)winform异步调用webservice
绕了一大圈,又开始接触winform的项目来了,虽然很小吧.写一个winform的异步调用webservice的demo,还是简单的. 一个简单的Webservice的demo,简单模拟服务 一个简单 ...
- 在 Windows Phone 中,为 Grid 添加 Tilt 效果
在 Windows Phone 中,Tilt 效果是比较经典的效果,我们可以很简单的为按钮等控件添加这样的效果(使用 Windows Phone Toolkit 的Tilt 效果),但是,如果我们想要 ...
- Django admin美化插件suit应用[原创]
前言 由于比较懒,自己弄了一个用户验证,没有自己写后台,用了django自带的user认证,并通过admin直接进行管理,但默认的admin并不漂亮,于是使用了这个django-suit插件,效果对比 ...
- 11 个很少人知道但很有用的 Linux 命令
Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...
- Win10 UWP开发系列:实现Master/Detail布局
在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...
- 自己动手写计算器v1.1
这个改动主要是使用工厂模式替代了简单工厂模式,这样做的好处是如果以后我们要扩充其他运算时,就不用总是去修改工厂类, 这是可以采取工厂模式,主要是将原来简单工厂类的逻辑判断分离出来,将它作为一个借口,与 ...
- Jsp语法简介
1.JSP指令 jsp指令用来设置整个JSP网页想关闭的属性,如网页的编码和脚本语言等.常用的3种指令为page,include和taglib. 2.JSP声明 jsp声明用于声明JSP代表的Serv ...
- JSON字符串和对象 的转换
一 通过eval() 函数可以将JSON字符串转化为对象 var obj = eval('(' + str + ')'); 或者 var obj = str.parseJSON(); //由JSON ...