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 ...
随机推荐
- 【BZOJ 2916】 2916: [Poi1997]Monochromatic Triangles (容斥)
2916: [Poi1997]Monochromatic Triangles Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 310 Solved: 1 ...
- 「HAOI2015」按位或
「HAOI2015」按位或 解题思路 : 这类期望题一眼 \(\text{Min-Max}\) 容斥,只需要稍微推一下如何求 \(E(minS)\) 即可. \[ E(minS) = \frac{1} ...
- HDU 1057 What Are You Talking About trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词. 翻译文段部分get ...
- Centos 右上角面板里没有wired network图标的问题
开了很多的网页查看解决这个问题,都不是很有效,最后很简单的改了下一个文件就ok了,自己记录下,以免忘记! 打入命令:sudo gedit /etc/NetworkManager/nm-system-s ...
- sqlserver -- 学习笔记(一)自定义函数(学习总结,备忘)
SQL Server自定义函数,以前只在书上看过,没有动手去敲一敲,今天刚好接触到,看了几篇博文学习了下.做好备忘很重要!! (@_@)Y Learn from:http://www.cnblogs. ...
- sklearn中的投票法
投票法(voting)是集成学习里面针对分类问题的一种结合策略.基本思想是选择所有机器学习算法当中输出最多的那个类. 分类的机器学习算法输出有两种类型:一种是直接输出类标签,另外一种是输出类概率,使用 ...
- 如何解决IIS7上传文件大小限制,.NET 上传文件后 找不到目录解决
IIS7 默认文件上传大小是30M,那么超过30M的文件就无法上传了,那么就需要对IIS的配置文件进行修改. 在实际应用中往往会出现上传文件太大,无法上传的情况,那是因为IIS对上传文件大小有限制,I ...
- keystone 命令简要说明
catalog: keystone catalog 可以显示所有已有的service keystone catalog --service service-type 显示某个service信息 end ...
- JLink v8克隆版破解向导
JLink v8克隆版破解向导 摘要 Jlink 4.5版本之后驱动会识别老的克隆版的JlinkV8,Jlink软件在启动时会提示为克隆版本后退出. 目前主流的破解方式主要有两种: 方法一,继续使用老 ...
- cadence学习(1)常规封装的建立
1.建立焊盘. (1)首先要获得datasheet(或可用pcb matrix ipc-7531标准的可查询封装软件)中元器件的封装信息. (2)建立.pad文件.打开PCB Editor Utili ...