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 ...
随机推荐
- 线程池(1)ThreadPoolExecutor梳理
使用默认的 thread factory创建ThreadPoolExecutor实例 public ThreadPoolExecutor(int corePoolSize, int maximumPo ...
- CoreCLR
CoreCLR 在这篇中我将讲述GC Collector内部的实现, 这是CoreCLR中除了JIT以外最复杂部分,下面一些概念目前尚未有公开的文档和书籍讲到. 为了分析这部分我花了一个多月的时间,期 ...
- (转) RHEL7 忘记密码修改root密码
博客链接:http://blog.csdn.net/derkampf/article/details/54346516 RHEL7进入单用户方式和重置密码方式发生了较大变化,GRUB由b引导变成了ct ...
- SQL生成日期维度(到小时)
#建表语句: CREATE TABLE [dbo].[Dim_日期3]( ) NOT NULL, [年] [int] NULL, ) NULL, ) NULL, ) NULL, ) NULL, ) N ...
- 安卓H5软键盘遮挡输入框
由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况) 1.input下方还有多余空位 ...
- Unity使用 转载
创建空的ASP.NET MVC3项目,添加对Unity2.0动态库的引用. 方法1:在MSDN上下载Untity2.0,安装后,默认安装在C:\Program Files\Microsoft Unit ...
- C#、VSTO讀取Excel類
之前寫的類存在Excel進程不能結束的Bug,重寫ExcelReader類,類實例清理時Excel進程自動結束. class ExcelReader { // Excel Object public ...
- 如何构建多模块的SpringBoot项目
通过阅读本文你将了解到:如何将已有SpringBoot项目改成多模块 & 如何新构建多模块SpringBoot项目 以下示例基于我正在使用的order(订单服务)进行演示,无论你用的是什么项目 ...
- IDEA对比文件
和另一个文件对比:右击文件,选择另一个文件 和剪切板的内容对比:右击代码区域
- 超文本传输协议(HTTP)
超文本传输协议(HyperText Transfer Protocol,Http)是从服务器传输数据到客户端的传输协议. HTTP协议的主要特点可概括如下: 1.支持客户/服务器模式. 2.简单快速: ...