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.

Solution 1:

class NumArray {

    int[] prefix;
public NumArray(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
prefix = new int[nums.length];
prefix[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
prefix[i] = prefix[i - 1] + nums[i];
}
} public int sumRange(int i, int j) {
if (i == 0) {
return prefix[j];
}
return prefix[j] - prefix[i - 1];
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/

Solution 2:

class NumArray {
int[] prefix;
public NumArray(int[] nums) {
prefix = new int[nums.length + 1];
for (int i = 0; i < nums.length; i++) {
prefix[i + 1] = prefix[i] + nums[i];
}
} public int sumRange(int i, int j) {
return prefix[j + 1] - prefix[i];
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/

[LC] 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. 303. Range Sum Query - Immutable

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

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

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

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

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

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

随机推荐

  1. 关于自动化打包部署Jenkins的使用和配置

    (未写完整,待续...) 名词解释: 1.Jenkins中对svn进行操作,可通过插件和脚本两种方式进行. 插件方式 在插件管理中安装"Subversion Plug-in",即可 ...

  2. Java 面向对象 异常处理:RunTimeexception,try-catch,异常声明throws,自定义异常,throw和throws的区别,多异常处理(9)

    Java 面向对象 异常处理:RunTimeexception,try-catch,异常声明throws,自定义异常,throw和throws的区别,多异常处理(9)

  3. java 环境变量配置搭建(1)

    基础常识,classPath配置,朱姐,跨平台性,world组成部分

  4. java查看简单GC日志

    测试代码: public class GCtest { public static void main(String[] args) { for (int i = 0; i < 10000; i ...

  5. C++对象数组与对象指针

    (一)对象数组 将具有相同类类型的对象有序地集合在一起便构成了对象数组,以一维对象数组为例,其定义形式为: 类名 对象数组名[]; Point points[100]; 关于对象数组的几点说明: (1 ...

  6. AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'

    今天爬虫时需要使用到selenium, 使用pip install selenium进行安装. 可是一开始写程序就遇到了AttributeError: module 'selenium.webdriv ...

  7. memset的常见用法

    头文件 <cstring> 描述 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组 ------------------------------------ ...

  8. 基于ssh开发彩票购买系统的设计与实现毕业设计

    开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MYSQL数据库 运行效果图: 源码及原文地址:http://javadao.xyz/forum.php?mod ...

  9. 常用DIV+CSS命名大全集合

    一.命名规则说明:   -   TOP 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",i ...

  10. 思考题:clock类编写

    题目 为便于后文理解,这里先补上这份代码前文开的库以及宏定义: #include<cstdio> #include<iostream> #include<string&g ...