【一天一道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 ...
随机推荐
- 【LSGDOJ 2015】数页码
题目描述 一本书的页码是从 1-n 编号的连续整数:1, 2, 3, ... , n.请你求出全部页码中所有单个数字的和,例如第 123 页,它的和就是 1+2+3=6. 输入 一行为 n(1 < ...
- ●BZOJ 3676 [Apio2014]回文串
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...
- 【Codeforces Round #430 (Div. 2) A C D三个题】
·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意: 给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...
- 全排列hash-康拓展开
这是对很多全排列问题适用的方法,而且还能用于一些题目的判重 第一位是3,当第一位的数小于3时,那排列数小于321 如 123. 213 ,小于3的数有1.2 .所以有2*2!个.再看小于第二位2的:小 ...
- [BZOJ]1095 Hide捉迷藏(ZJOI2007)
一道神题,两种神做法. Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特 ...
- matplotlib中subplot的各参数的作用
subplot(a,b,c)中a代表所画图形的行数 b代表所画图形的列数 c代表所画图形的序号. plt.figure(facecolor='w', figsize=(9, 10)) plt.subp ...
- 一个小小的抽奖活动测试脚本(python2.7)
# coding=utf-8import requestsimport cx_Oracletns=cx_Oracle.makedsn('172.30.0.155',1521,'szdev')db1=c ...
- Spring系列之装配Bean
Spring 的三种装配Bean的方式 组件扫描+自动装配(隐式) 通过Java config装配bean(显示) 通过XML装配bean(显示) 一.组件扫描+自动装配(隐式配置) 组件扫描: Sp ...
- 《Java技术》第三次作业
(一)学习总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Gr ...
- jvm(一):总体概述
我们首先来了解一下jdk,jre,jvm的之间的关系 jvm用于运行字节码,如果我们仅仅用于运行java程序,仅部署jre即可,如果我们需要进行java开发则需要jdk环境 java结构内容: jav ...