【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
我的个人博客已创建,欢迎大家持续关注!
一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客
另外,本系列文章已整理并上传至gitbook,网址:点我进
欢迎转载,转载请注明出处!
(一)题目
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.
(二)解题
题目大意:给定一个数组,计算它的区间和
解题思路:本题给出了NumArray的构造函数,区间和应该再构造函数内就已经计算好。
class NumArray {
public:
NumArray(vector<int> &nums) {
int size = nums.size();
int sum = 0;
for(int i = 0 ; i < size ; i++){
vec.push_back(sum);//vec里面存放第0~i-1位数的和。
sum+=nums[i];
}
vec.push_back(sum);//第0~i位的和需要压入vector
}
int sumRange(int i, int j) {
return vec[j+1] - vec[i];//直接计算区间和
}
public:
vector<int> vec;
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
【一天一道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(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...
- 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
问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
随机推荐
- CSAPP-链接
主要任务: 1.符号解析 在声明变量和函数之后,所有的符号声明都被保存到符号表. 而符号解析阶段会给每个符号一个定义. 2.重定位: 把每个符号的定义与一个内存位置关联起来,然后修改所有对这些符号的引 ...
- APIO 2015
老师让我们打这套题练练手.感觉这套题还是挺有意思的,比国内某些比赛不知道高到哪里去.最后我拿了284/300,貌似比赛是IOI赛制啊,强行被当成OI赛制做了,不然我T3可能还能多骗点. T1.scul ...
- ●ZOJ 2112 Dynamic Rankings
●赘述题目 对于一个长为n(n<50000)的序列(序列中的数小于1000000000),现有如下两种指令: Q a b c:询问区间[a,b]中第c小的数. C p b:将序列中的从左往右数第 ...
- Excel实现双击插入当前日期时间
用vba做一个事件驱动程序. 第一步:点击开发工具-visual basic. 第二步:在当前工作表中编制程序如下: Private Sub Worksheet_BeforeDoubleClick(B ...
- 如何彻底删除mysql
MySQL的卸载确实很让人头疼,很多时候都无法彻底卸载干净,这样会导致我们无法重新安装新的MySQL. 下面介绍,在Windows10系统下,如何彻底删除卸载MySQL... 1>停止MySQL ...
- Spring MVC页面重定向
以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面. 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 ...
- html5应用程序缓存
缓存概念: ------页面缓存: html.JS.CSS等,这些缓存资源是由于浏览器的行为而产生; ------数据缓存 ----------AppCache: Cache Manifest 操作 ...
- 数组中存放对象之java中定义类数组存放类
public class ClassArrayDemo { int age; String name; ClassArrayDemo(int age, String name) { this.age ...
- Error:Cannot build Artifact :war exploded because it is included into a circular depency
找到项目的目录 查找artifacts文件夹 删掉不是你项目名称的那个 问题出现的原因是你该项目名字了 造成tomcat发布两个网页 发布两个网页不是什么大问题 但是这两玩意地址一样 争夺资源啊冲突之 ...
- angular+ionic前后端分离开发项目中的使用
Ionic基于AngularJS构建而成,所以学习一些AngularJS的知识很有必要.Ionic并没有独立开发一套完整的Web应用框架,而是对AngularJS进行了扩展,给它添加了大量界面组件和其 ...