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:
You may assume that the array does not change.
There are many calls to sumRange function.
public class NumArray {
private static int[] nums;
private static int[] sums;
public NumArray(int[] nums) {
if(nums==null)
return ;
this.nums = nums;
sums=new int[nums.length];//注意为什么不nums=sums,这样的话跟着变,这样的话原始数组也会变,这样不科学
if (nums.length == 1) {
sums[0] = nums[0];
return;
}
for (int x = 1; x < nums.length; x++) {
sums[x] = sums[x - 1] + nums[x];
}
} public int sumRange(int i, int j) {
return sums[j]-sums[i]+nums[i];
}
} // Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
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 解题报告(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),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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 ...
- 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 (C++)
题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...
随机推荐
- asp.net 计算两个时间差
两个时间相差多少 .net中的timespan应用2008/11/10 11:54TimeSpan 对象表示时间间隔或持续时间,按正负天数.小时数.分钟数.秒数以及秒的小数部分进行度量.用于度量持续时 ...
- linux 下 nginx 启动服务器 80端口被占用问题
把80端口占用的程序杀死 sudo fuser -k 80/tcp rm -fr 文件 ----删除文件及文加下的所有文件 echo > filename ---清空文件的内容
- 转: Ext拖拽分析
整个Ext架构中组件是其重要的组成部分,除了少部分(如树的结点)的界面表现元素不是在这样的一个体系中,大部分的页面表现元素都被绑定在这个体系之中,下面从这个体系的最底层即在这个继承体系的最高层进行研究 ...
- [mysql] mysql主从复制(基于日志点)
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修 ...
- [mysql] mysql 5.6.X 慢查询日志
慢查询日志 一篇好文章,学习保存.... 打开慢查询日志 慢查询日志,顾名思义就是记录执行比较慢查询的日志. 查看是否开启慢查询日志: show variables like '%slow%'; 打开 ...
- android学习笔记18——dpi、dp、sp、xp......
参考:http://www.cnblogs.com/greatverve/archive/2011/12/28/android-dip-dp-sp-pt-px.html http://www.360 ...
- Python 结巴分词(2)关键字提取
提取关键字的文章是,小说完美世界的前十章: 我事先把前十章合并到了一个文件中: 然后直接调用关键字函数: import sys sys.path.append('../') import jieba ...
- 2011-2015年Journal of Mathematical Physics高引用文章
http://aip-info.org/1XPS-45FE9-0AC9S389C5/cr.aspx
- ylbtech-dbs:ylbtech-2,PAM(个人资产管理系统)
ylbtech-dbs:ylbtech-2,PAM(个人资产管理系统) -- =============================================-- Personal Asse ...
- Linux Shell入门(转载)
From:http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用 ...