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.
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.
分析
给定一个整数序列,求指定子序列和。
提示:数组不会发生变化;大量sumRange函数调用。
题目本身非常简单,只需要遍历 i 到 j ,累计得到和即可。但是,这样是TLE的,所给提示也就没有意义了。
所以,题目考察的是效能,换一个方向思考,我们可以存储子序列和,每个下标处的值为[0,i]的所有元素和;
那么[i,j]子序列和 =sum[j]−sum[i−1];
注意,i==0时,直接返回sum[j]即可。
AC代码
class NumArray {
public:
NumArray(vector<int> &nums) {
if (nums.empty())
return ;
else
{
sums.push_back(nums[0]);
//求得给定数列长度
int len = nums.size();
for (int i = 1; i < len; ++i)
{
sums.push_back(sums[i - 1] + nums[i]);
}//for
}
}
//计算[i,j]序列和
int sumRange(int i, int j) {
if (0 == i)
return sums[j];
int len = sums.size();
if (i < 0 || i >= len || j < 0 || j >= len || i > j)
{
return 0;
}//if
return sums[j] - sums[i-1];
}
private:
//存储数列和
vector<int> sums;
};
LeetCode(303)Range Sum Query - Immutable的更多相关文章
- LeetCode(304)Range Sum Query 2D - Immutable
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- leetcode@ [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable
https://leetcode.com/problems/range-sum-query-immutable/ class NumArray { public: vector<int> ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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_303. Range Sum Query - Immutable
303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- Netty(5)@Sharable
问题:我写了MyDecoder which extends ByteToMessageDecoder,单线程没问题,但是多线程时,报'the handler should be sharable'.查 ...
- 告别JQuery(一)
背景 很多很多传统的Web开发者还在用着传统的jquery和ES5,大家都知道现在的前端如火如荼,但是眼花缭乱的框架和层出不穷的新概念,让很多人无从下手,本文从0开始,带你一步步由jquery操作DO ...
- linux文件系统和目录树的关系
文件系统是和底层的硬件系统紧密关联的,文件系统相当于是dev(设备或硬件)在Linux上面的显示,如/dev/hdc2 而目录树是逻辑的概念,其可以通过挂载的方式连接文件系统,先用df查看本地的文件系 ...
- AngularJS(二):ng-app指令、表达式
本文也同步发表在我的公众号“我的天空” ng-app指令 AngularJS指令是扩展的HTML属性,所有指令均带有前缀“ng-”,我们学习的第一个指令便是ng-app,其定义了AngularJS管理 ...
- css继承性
不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...
- Kendo UI 单页面应用(二) Router 类
Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...
- calibre电子书管理软件
软件介绍: Calibre 是电子书管理软件,支持 Amazon.Apple.Bookeen.Ectaco.Endless Ideas.Google/HTC.Hanlin Song 设备及格式,功能十 ...
- 前端HTML以及HTML5(基本标签)
前面一章介绍了一下前端的发展,这章简单介绍一下html的发展以及基本的标签. 一.HTML的发展史 1.概念 超文本标记语言(HyperText Markup Language,简称HTML)是为 [ ...
- for循环/计算坐标
for循环计算坐标 webqq里面有类似桌面的各种图标,是绝对定位的,这样可以拖动改变位置,用浮动的话,没法拖动. <!DOCTYPE html> <html lang=" ...
- 简单案列完美搞定Mvc设计模式
一个小列子搞定Mvc模式,包括数据库以及如何提高用户体验度 1.首先来web.xml配置servlet的访问路径: <?xml version="1.0" encoding= ...