303 Range Sum Query - Immutable 区域和检索 - 不可变
给定一个数组,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
例如:
给定nums = [-2, 0, 3, -5, 2, -1],求和函数为sumRange()
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
注意:
你可以假设数组不可变。
会多次调用 sumRange 方法。
详见:https://leetcode.com/problems/range-sum-query-immutable/description/
C++:
方法一:
class NumArray {
public:
NumArray(vector<int> nums) {
dp=nums;
for(int i=1;i<nums.size();++i)
{
dp[i]+=dp[i-1];
}
}
int sumRange(int i, int j) {
return i==0?dp[j]:dp[j]-dp[i-1];
}
private:
vector<int> dp;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
方法二:
class NumArray {
public:
NumArray(vector<int> nums) {
dp.resize(nums.size()+1,0);
for(int i=1;i<=nums.size();++i)
{
dp[i]=dp[i-1]+nums[i-1];
}
}
int sumRange(int i, int j) {
return dp[j+1]-dp[i];
}
private:
vector<int> dp;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
参考:https://www.cnblogs.com/grandyang/p/4952464.html
303 Range Sum Query - Immutable 区域和检索 - 不可变的更多相关文章
- [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] 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] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
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 ...
随机推荐
- hdu5371 Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- Maven具体解释之仓库------本地仓库、远程仓库
在Maven中,不论什么一个依赖.插件或者项目构建的输出.都能够称之为构件. Maven在某个统一的位置存储全部项目的共享的构件.这个统一的位置.我们就称之为仓库.(仓库就是存放依赖和插件的地方) 不 ...
- 传智播客ADO.Net项目开发教程具体解释
内容简单介绍: 本教程为传智播客.Net培训课堂的现场录像,请到140623ls" target="_blank">传智播客.Net学院下载很多其它免费.Net视频 ...
- 兔子--CheckBox与Radiobutton的差别
RadioButton和CheckBox的差别: 1.单个RadioButton在选中后.通过点击无法变为未选中状态,单个CheckBox在选中后.通过点击能够变为未选中. 2.一组RadioButt ...
- Python正則表達式小结(1)
学习一段python正則表達式了, 对match.search.findall.finditer等函数作一小结 以下以一段网页为例,用python正則表達式作一个范例: strHtml = '''& ...
- 【bzoj1150】[CTSC2007]数据备份Backup
将k对点两两相连,求最小长度 易证得,最优方案中,相连的办公楼一定是取相邻的比取不相邻的要更优 然后就可以用贪心来做这道题了.. 将初始所有的线段放进堆里 每次取最短的线段进行连接,且ans+=a[i ...
- 开源框架 KJFrameForAndroid
一个Android的快速开发工具包,使用它你可以轻松实现网络请求.插件化开发.图片加载等功能.KJFrameForAndroid的设计思想是通过封装Android原生SDK中复杂的复杂操作而达到简化A ...
- POJ2749 Building road
传送门 这道题真是2-SAT好题啊!!卡了我两个点才做完……垃圾POJ还不告诉我哪错了…… 首先我们先花一段时间把题看懂……(其实是翻译一下),之后我们发现因为每个谷仓只能向一个中转点连边,所以他就是 ...
- POJ3528 HDU3662 三维凸包模板
POJ3528 HDU3662 第一道题 给定若干点 求凸包的表面积,第二题 给定若干点就凸包的面数. 简单说一下三维凸包的求法,首先对于4个点假设不共面,确定了唯一四面体,对于一个新的点,若它不在四 ...
- gitweb
1. 简介 Gitweb提供了git版本库的图形化web浏览功能.可以到网站http://git.kernel.org/体验下效果,如下图所示. Gitweb界面 它既可以通过配置架设于web服务器上 ...