LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译
给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和。
比如:
给定nums = [-2,0,3,-5,2,-1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
批注:
你能够假定这个数组不会改变。
这里会有非常多次对sumRange函数的调用。
原文
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.
分析
一開始我还以为这个题有多简单呢,就写了以下这个代码:
class NumArray {
public:
vector<int> numArray;
NumArray(vector<int> &nums) {
numArray = nums;
}
int sumRange(int i, int j) {
int sum = 0;
while (i < j) {
sum += numArray[i++];
}
return sum;
}
};
由于不知道须要构造函数干嘛,所以硬生生的加了这么一句进去,事实上全然没用。
功能是实现了。可是面对题目变态的測试用例,果然还是崩了。
至于题目測试用例有多变态,数据来说话:一共214513个字符,当中包含了nums的数据,也包含了函数调用,nums的长度没去算,可是sumRange函数的调用次数高达10000次。
看到这个測试用例,就明确要针对函数调用做优化,一个不错的办法是将全部的返回的值:
从0到1的全部元素之和,
从0到2的全部元素之和,
从0到10000的全部元素之和,
………………
然后计算i和j之间全部元素之和的时候,(由于包含了i和j),也就是求从0到j的全部元素之和减去从0到i-1的全部元素之和。
所以代码就例如以下所看到的了。可是时间上还是使用了624ms叻。
class NumArray {
public:
map<int, int> rangeSum;
NumArray(vector<int> &nums) {
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
rangeSum.insert(pair<int, int>(i, sum));
}
}
int sumRange(int i, int j) {
return rangeSum[j] - rangeSum[i - 1];
}
};
尽管对底层的详细实现不是非常了解。但由于有索引,所以我自觉用vector的话查找其值来跟高速;并且加入值也更加高速,由于仅仅是一个值而不用是键值对。
详细代码例如以下:
代码
class NumArray {
public:
vector<int> rangeSum;
NumArray(vector<int> &nums) {
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
rangeSum.push_back(sum);
}
}
int sumRange(int i, int j) {
return rangeSum[j] - rangeSum[i - 1];
}
};
LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)的更多相关文章
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 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 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- Java [Leetcode 303]Range Sum Query - Immutable
题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- LeetCode 303. Range Sum Query - Immutable (C++)
题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...
- 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】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- Highcharts实现走势图
Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用.HighCh ...
- Angular Material Starter App
介绍 Material Design反映了Google基于Android 5.0 Lollipop操作系统的原生应用UI开发理念,而AngularJS还发起了一个Angular Material ...
- 洛谷P1149 火柴棒等式
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1.加号与等号 ...
- POJ 3553 Light Switching Game 博弈论 nim积 sg函数
http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...
- 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels
题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...
- 【四边形不等式】HDU3506-Monkey Party
[题目大意] 香蕉森林里一群猴子(n<=1000)围成一圈开会,会长给他们互相介绍,每个猴子需要时间a[i].每次只能介绍相邻的两只猴子x和y认识,同时x所有认识的猴子和y所有认识的猴子也就相互 ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- redis配置参数简介
redis配置查看方式: 1.redis的安装目录查看redis.conf 2.登陆redis客户端,使用 config get xx命令. 比如:查看所有的配置: config get * [roo ...
- ExtJS4.1 ExtJS TabPanel 双击标签关闭该页
/*总觉得 TabPanel 生成的那个关闭按钮太小关闭的时候不太顺手 感觉不方便 所以想双击关闭tab方便些于是在网上找到下面的代码 URL:http://atian25.iteye.com/blo ...
- oc/object-c/ios用int还是NSInteger
当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的. 可以看如下定义:#if _ ...