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 ...
随机推荐
- Am335x U-boot LCD简易驱动
参考此文档说明,自行添加相关代码: https://pan.baidu.com/s/1i5gLE89 相关代码: https://pan.baidu.com/s/1qXL8Bne 在文档说明第四步1中 ...
- Codeforces 806 D.Prishable Roads
Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...
- 【Trie图】BZOJ3940-[Usaco2015 Feb]Censoring
[题目大意] 有一个匹配串和多个模式串,现在不断删去匹配串中的模式串,求出最后匹配串剩下的部分. [思路] 众所周知,KMP的题往往对应着一道AC自动机quq.本题同BZOJ3942(KMP),这里改 ...
- Android UI框架基本概念
Activity:基本的页面单元,Activity包含一个Window,window上可以绘制各种view View:最基本的UI组件,表示屏幕上的一个矩形区域: Window:表示顶层窗口,管理界面 ...
- HTTP状态码,400,404,500,503
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 400 服务器不理解请求的语法 404 - 请求的网页不存在 503 - 服务不可用 所有状态 ...
- 51nod 1035 最长的循环节 数学
1035 最长的循环节 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1035 Description 正整 ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- SSM+Maven(教程二):Idea快速入门SSM+Maven框架。
快速入门须知 这篇文章,直接应用已经搭建好的SSM框架.一般在公司里面,考虑框架的搭建.封装等问题,都由研发经理或者架构师完成,所以对于刚入门的小白来说,在去搭建整合花费的时间会很多很多.对于理解能力 ...
- JMX操作实例--做一回技术控
我来做一回技术控,这部分内容也是简单的API调用例子而已,做一回技术控,发点小骚文,不过你看了,也许知道JConsole是怎么做出来的了,呵呵! 先不用管他干什么,代码运行后,自己改改自然知道做什么的 ...
- Druid 配置_StatFilter
Druid内置提供一个StatFilter,用于统计监控信息. 1. 别名配置 StatFilter的别名是stat,这个别名映射配置信息保存在druid-xxx.jar!/META-INF/drui ...