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 ...
随机推荐
- mysql 修改字符集
查看mysql数据编码 登录MySQL服务,查看mysql数据库默认的编码 mysql> show variables like 'character%'; +----------------- ...
- centos6.5 安装iptables
阿里云默认是没有安装iptables 安装 yum install -t iptables yum install iptables-services 检查iptables服务的状态 service ...
- Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法
Ext JS treegrid 发生的在tree上增加itemclick 与在其它列上增加actioncolumn 发生事件冲突(event conflict)的解决办法 最近在适用Ext JS4开发 ...
- 安卓ROOT后禁用/隐藏导航栏/虚拟按键
安卓ROOT后禁用/隐藏导航栏/虚拟按键 提醒:提前装好EASY TOUCH 等类似工具. 用ROOT EXPLORER 或 ROOT BROWSER system\bulid.prop 最后加一行: ...
- LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
- Centos5.5下安装cacti
系统环境OS:CentOSDataBase:MySQL5.0PHP Apachenet-snmp部署CentOS cacti配置需要的环境安装MySQLyum -y install mysqlyum ...
- Puppet's Commands 3.7
Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...
- android学习笔记37——Menu资源
Menu菜单资源 android应用推荐使用XML来定义菜单,其可提供更好的解耦方式. 菜单资源通常位于res/menu文件夹下,其菜单根元素为<menu.../>,menu元素下可包含子 ...
- TMS320C54x系列DSP的CPU与外设——第5章 数据寻址
第5章 数据寻址 C54x DSP提供7种基本寻址方式. ■ Immediate addressing uses the instruction to encode a fixed value. ...
- Android四:sqllite
1.扩展类SQLiteOpenHelper xxSQLiteHelper extends SQLiteOpenHelper public xxSQLiteHelper(Context context, ...