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的更多相关文章

  1. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  2. [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 ...

  3. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  4. 【leetcode❤python】 303. Range Sum Query - Immutable

    #-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...

  5. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  6. 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 ...

  7. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  8. 303. Range Sum Query - Immutable(动态规划)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. 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 ...

随机推荐

  1. MySQL下载、安装和修改root密码

    一.下载地址:MySQL_5.6.22_winx64_XiaZaiBa :http://rj.baidu.com/soft/detail/12585.html?ald 二.安装软件,安装到指定的路径, ...

  2. Oracle Group by+rollup+cube 的应用

    首先我们创建一个示例表: Create table test_group (v_name varchar2(4) ,v_size varchar2(4) ,v_color varchar2(4) ,n ...

  3. oracle11gRAC环境使用RMAN增量备份方案

    转摘:http://blog.itpub.net/29819001/viewspace-1320977/ [oracle@zx ~]$ rman target /Recovery Manager: R ...

  4. Android 服务端开发之开发环境配置

    Android 服务端开发之开发环境配置 这里是在Eclipse的基础上安装PhpEclipse插件方法,PHPEclipse是Eclipse的 一个用于开发PHP的插件.当然也可以采用Java开发a ...

  5. 【shiro】org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token

    org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.sh ...

  6. mysql批量更新

    UPDATE ta INNER JOIN tb ON ta.id=tb.id SET ta.col1=tb.col1, ta.col2=tb.col2 以上代码用来批量更新mysql中的记录

  7. Neutron分析(7)—— neutron-l3-agent HA solutions

    1. keepalived vrrp/conntrackd High availability features will be implemented as extensions or driver ...

  8. WPF4.5 中的新增功能和增强功能的信息

    本主题包含有关 Windows Presentation Foundation (WPF) 版本 4.5 中的新增功能和增强功能的信息. 本主题包含以下各节: 功能区控件 改善性能,当显示大时设置分组 ...

  9. 【mysql】SQL常用指令

    常用操作指令 show databases;显示所有的数据库: use dbName; 使用指定数据库 show tables; 显示所有的数据表: desc tableName; 查看数据表的字段信 ...

  10. ubuntu ipv6网络电视(avplay)

    首先在ubuntu下安装好ipv6 (话说是已经装好了的,不过最好检查以下) 网上有很多资源,我不写了. 测试一下 :ping ipv6.scau.edu.cn 另外,关于ipv6 网络播放器很多人推 ...