303. Range Sum Query - Immutable(动态规划)
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:
1: You may assume that the array does not change.
2: There are many calls to sumRange function.
分析:
由于开始是写PAT现在写leetcode提交方式很多不一样,这题提交方式看得我很懵逼啊,于是搜了一下什么意思;这道题首先求和了从0到i(0<=i<=n),然后如果求sum(i,j),如果i==0,
则输出v[j],如果i!=0,输出v[j]-v[i-1];
class NumArray {
private:
vector<int> v;
public:
NumArray(vector<int> nums) {
if(nums.size()==0)
return ;
v.push_back(nums[0]);
for(int i=1;i<nums.size();i++)
v.push_back(v[i-1]+nums[i]);
}
int sumRange(int i, int j) {
if(i==0)
return v[j];
return v[j]-v[i-1];
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
303. Range Sum Query - Immutable(动态规划)的更多相关文章
- 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 (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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- Android中关于横竖屏问题
一.屏幕的方向可以在AndroidManifest.xml文件中通过设置screenOrientation属性来实现, screenOrientation用来指定Activity的在设备上显示的方向, ...
- 使iframe随内容(target到iframe的内容)改变而自适应高度,完美解决各种获取第一个demo高度后第二个高度不变情况
转自:http://caiceclb.iteye.com/blog/281102 很高兴,终于使用jquery实现了点击外部链接,更改iframe内容时,iframe的高度自适应问题. 失败的测试就不 ...
- P3178 [HAOI2015]树上操作 树链剖分
这个题就是一道树链剖分的裸题,但是需要有一个魔性操作___编号数组需要开longlong!!!震惊!真的神奇. 题干: 题目描述 有一棵点数为 N 的树,以点 为根,且树点有边权.然后有 M 个操作, ...
- IJ:IJ笔记1
ylbtech-IJ:IJ笔记1 1. 下拉框返回顶部 1. <form:select id="type" path="baseId" class=&qu ...
- Secure CRT中解决vim高亮设置的方法
此文主要是解决vim编程中高亮显示的.原因是: 1.默认情况下,SecureCRT是有自己的终端显示颜色.这样在我们编程中不利于阅读内容. 2.我们必须到Linux系统中进行改进才能真正解决这样的问题 ...
- 【RAID在数据库存储上的应用 】
随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...
- PCB genesis Slot槽转钻孔(不用G85命令)实现方法
PCB钻Slot槽一般都采用G85命令钻槽孔,而采用G85命令工程CAM无法准确的知道Slot槽钻多少个孔,并不能决定钻槽孔的顺序,因为采用G85命令钻孔密度与钻槽顺序由钻机本身决定的.在这里介绍一种 ...
- Java中多个线程交替循环执行
有些时候面试官经常会问,两个线程怎么交替执行呀,如果是三个线程,又怎么交替执行呀,这种问题一般人还真不一定能回答上来.多线程这块如果理解的不好,学起来是很吃力的,更别说面试了.下面我们就来剖析一下怎么 ...
- Java使用Player播放mp3
大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...
- Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问
总算是弄出来了,先写下来供自己以后查阅. 1)首先你要有一个阿里云服务器,我用的是Centos7学生认证,10元/月,很便宜也很好用. 2)购买了域名,首年9元,很划算.域名买来之后经历了拍照备案,前 ...