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 ...
随机推荐
- User32.dll详细介绍
RegisterServiceProcess(ProcessID:Long,Type:Long) 该函数存在于Kernal32.dll中. Process指向进程的ID,Type表示是否向系统注册该进 ...
- luogu2744 量取牛奶
题目大意 给出一个整数集合$A$,总数$N$,规定一个整数序列$\{a_n\}, \forall i, a_i\in A$满足条件:存在一个正整数序列$\{k_n\}$,使得$\sum_{i=1}^n ...
- ios11--UIButton
// // ViewController.m // 02-UIButton(在代码中使用) // #import "ViewController.h" @interface Vie ...
- C# 学习笔记 三层架构系列(控件一)
下面是我两周的学习总结:这是我写给自己的,如果哪位朋友有幸看到这篇文章就是缘分.如果所说的内容不对,就请纠正.勿喷!!! 想要将两周的学习知识通过文字.通过代码.通过图片储备起来,以防自己那天思维短路 ...
- go语言笔记——是c开发的 lex yacc进行词法和语法分析,go不支持函数和运算符重载,不支持类型继承,也不支持断言,还有泛型
从 Go 1.0.3 版本开始,不再使用 8g,8l 之类的指令进行程序的构建,取而代之的是统一的 go build 和 go install 等命令,而这些指令会自动调用相关的编译器或链接器. 如果 ...
- RDA 搜台
转载马斯特·李 流程: 将channel的读写回调在AL_FW_Init中注册 初始化datasaving部件,注册datasaving的回调,并建立DATASAVING_NvmStore_Threa ...
- IDEA 中Spark SQL通过JDBC连接mysql数据库
一.IDEA装驱动: 1.下载一个MySQL的JDBC驱动:mysql-connector-java-5.1.44.tar.gz2.在idea Open Moudle Settings 在 Moudl ...
- 72. js EXTJS grid renderer用法
转自:https://blog.csdn.net/shancunxiaoyazhi/article/details/22156083 renderer : Function (可选的)该函数用于加工单 ...
- IDEA新项目代码上传到gitlab远程仓库
IDEA新项目代码上传到gitlab远程仓库 具体步骤 创建本地仓库 IDEA:VCS-->Import into Version Control-->Create Git Reposit ...
- RabbitMQ死循环-延长ACK时间
一.应用背景 今天做一个需求,要将RabbitMQ中的任务取出并执行,为防止任务执行期间出错,设置NO_ACK=FALSE标志,这样.一旦任务没有应答的话,相应的任务就会被RabbitMQ自动Re-Q ...