303. Range Sum Query - Immutable

Easy

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.
package leetcode.easy;

class NumArray1 {
private int[] data; public NumArray1(int[] nums) {
data = nums;
} public int sumRange(int i, int j) {
int sum = 0;
for (int k = i; k <= j; k++) {
sum += data[k];
}
return sum;
}
} class NumArray3 {
private int[] sum; public NumArray3(int[] nums) {
sum = new int[nums.length + 1];
for (int i = 0; i < nums.length; i++) {
sum[i + 1] = sum[i] + nums[i];
}
} public int sumRange(int i, int j) {
return sum[j + 1] - sum[i];
}
} /**
* Your NumArray object will be instantiated and called as such: NumArray obj =
* new NumArray(nums); int param_1 = obj.sumRange(i,j);
*/
public class RangeSumQueryImmutable {
@org.junit.Test
public void test1() {
int[] nums = { -2, 0, 3, -5, 2, -1 };
NumArray1 obj = new NumArray1(nums);
int param_1 = obj.sumRange(0, 2);
int param_2 = obj.sumRange(2, 5);
int param_3 = obj.sumRange(0, 5);
System.out.println(param_1);
System.out.println(param_2);
System.out.println(param_3);
} @org.junit.Test
public void test3() {
int[] nums = { -2, 0, 3, -5, 2, -1 };
NumArray3 obj = new NumArray3(nums);
int param_1 = obj.sumRange(0, 2);
int param_2 = obj.sumRange(2, 5);
int param_3 = obj.sumRange(0, 5);
System.out.println(param_1);
System.out.println(param_2);
System.out.println(param_3);
}
}

LeetCode_303. Range Sum Query - Immutable的更多相关文章

  1. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

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

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

  3. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

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

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

  5. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

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

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

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

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

  8. Leetcode 303 Range Sum Query - Immutable

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

  9. [LeetCode] Range Sum Query - Immutable

    The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...

随机推荐

  1. windows nginx 目录配置

    http { server { listen 80; server_name www.test.com; location / { root E:/data/www; index index.html ...

  2. java之比较器

    java中的比较器有两种: 1.实体类实现Comparable接口,并实现其中的compareTo方法 2.在外部定义实现Comparator接口的比较器类,并实现其中的compare方法 Compa ...

  3. car购车翻译篇

    Sedans 4门轿车 si 运动型车,通常匹配6挡位变速箱 Coupes 双门,有少少跑车的含义 Hatchbacks 掀背   配置英语 Honda Sensing® Standard 感应标准, ...

  4. Linux下搭建iSCSI共享存储的方法 Linux-IO Target 方式 Debian9.5下实现

    iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...

  5. JS发送验证码;并设置cookie

    Tool.send_code = function(obj) { var isCheck = true, form = $('#editInfo_Form'); var mobile = form.f ...

  6. Pytest权威教程09-捕获标准输出及标准错误输出

    目录 捕获标准输出及标准错误输出 默认 stdout/stderr/stdin 捕获行为 设置捕获方法或禁用捕获 调试中使用print语句 在测试用例中使用的捕获的输出 返回: Pytest权威教程 ...

  7. CF1203F2 Complete the Projects (hard version)(结论+背包+贪心)

    题目 做法 对于加分的直接贪心 而掉分的用排序后的背包动规 假设有两个物品\((a_1,b_1)(a_2,b_2)\) 选第一个物品后无法选择第二个物品,假设开始值为\(r\):\(r>a_1, ...

  8. 浅析python-socket编程

    1. 什么是socket? socket是套接字的英文名称, 我们知道在TCP/IP协议簇体系中,将网络状态分为了应用层.传输层.网络层.物理层等四种状态,而socket是与传输层密切相关的,其主要实 ...

  9. ICEM-闪闪的党徽

    ​原视频下载地址:http://yunpan.cn/cusb64DXrammF  访问密码 3d0f

  10. Java通过JDBC连接MySQL数据库(一)

    JDBC JAVA Database Connectivity java 数据库连接 为什么会出现JDBC SUN公司提供的一种数据库访问规则.规范, 由于数据库种类较多,并且java语言使用比较广泛 ...