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 ...
随机推荐
- VB.NET学习体会
注:本文写于2018年01月28日,首先发表于CSDN博客"aopstudio的博客"上 下学期要学习VB.NET程序设计课程,这几天在家开始自习.在自习的过程中发现VB.NET和 ...
- SpringBoot2.0 浅谈注解@ControllerAdvice的作用
我们都知道做项目一般都会有全局异常统一处理的类,那么这个类在Spring中可以用@ControllerAdvice来实现,费话不多说,先看代码: import org.springframework. ...
- 洛谷2019 3月月赛 T3
题干 唯一AC T3 的大巨佬%%% 这题就是个大模拟吧. 题解
- ansible publishing service
# ansible 初始化服务机 - hosts: newserver vars: - basedir: opt tasks: - name: 安装常用依赖环境 yum: name={{ item } ...
- CF615C Running Track
思路: kmp + 二分. 实现: #include <iostream> #include <cstdio> #include <algorithm> #incl ...
- asp.net——统计输入的字符数目
asp.net——统计输入的字符数目 题目: 在页面中有一个TextBox输入框,一个显示文字用的Label,一个提交按钮Button.在TextBox中输入一段英文字母,点击按钮提交后统计其中字母‘ ...
- hdu2639,第K优决策
在dp问题中如果遇到问题,没有什么是加一维度不能解决的,如果不能,再加一维度. #include<iostream> #include<cstring> #include< ...
- Miller Rabin 大素数测试
PS:本人第一次写随笔,写的不好请见谅. 接触MillerRabin算法大概是一年前,看到这个算法首先得为它的神奇之处大为赞叹,竟然可以通过几次随机数据的猜测就能判断出这数是否是素数,虽然说是有误差率 ...
- sql server 数据库跨服务器备份,复制监视器——快照代理,复制流程
在做数据库跨服务器复制时,查看复制监视器的快照代理,可以看到复制流程,具体如下: 初始化 连接发布服务器 设置服务器数据库兼容性级别 更新索引的统计信息 在生成快照时锁定已发布的表 复制快照数据(每个 ...
- SQL基本操作——事务
事务是并发和恢复控制的基本单元. 事务四个属性:原子性.一致性.隔离性.持久性. 原子性:一个事务是一个不可分割的单位,事务中包括的诸多操作要么成功要么都失败. 一致性:事务必须使数据库从一个一致性状 ...