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 ...
随机推荐
- centos虚拟机安装指定版本docker
环境: centos 7.6+ docker-ce 17.03.2 安装依赖包 yum -y install yum-utils device-mapper-persistent-data lvm2 ...
- 如何给数组用fill函数和memset函数给数组赋初值
fill是按照单元来赋值的,所以可以填充一个区间的任意值 #include<iostream> #include<stdio.h> #include<string.h&g ...
- Funsioncharts 线图 破解
在线示例:http://jsfiddle.net/henley/xnozyLa8/2/ 下载:http://files.cnblogs.com/files/ycdx2001/chart.zip
- Spark Mllib里决策树回归分析使用.rootMeanSquaredError方法计算出以RMSE来评估模型的准确率(图文详解)
不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型 ...
- h5复制粘贴板,打开APP功能
<div class="container"> <img src="../themes/mall/img/i_red_ad4.jpg"> ...
- [20190614]webpack+vue学习记录
本文记录一些学习webpack+vue相关的知识点,方便以后查阅,添加或修改 1. 初始化vue项目的代码结构 build--项目依赖包配置信息 config--项目配置文件 dev.env.js-- ...
- react中constructor和super()以及super(props)的区别。
react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...
- Android(java)学习笔记115:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- springboot(二十一)-集成memcached
Memcached 介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...
- 2018.3.27 Mac 配置Tomcat
先在官网上下载Tomcat .也可以用这个传送门. https://tomcat.apache.org/download-70.cgi 选择zip文件夹的下载就ok 下载完成之后将该文件夹.(如果是t ...