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. Web开发技术---简单的登录验证

    制作一个APP或系统最基础的是登录界面,下面通过一个简单的登录注册界面的程序,来熟练掌握Web开发的技术. 一.知识点: 1.在网页界面获取用户的输入信息 2.标签的基本应用 3.用户输入结果的错误提 ...

  2. javaweb学习笔记(二)

    一.javaweb学习是所需要的细节 1.Cookie的注意点 ① Cookie一旦创建,它的名称就不能更改,Cookie的值可以为任意值,创建后允许被修改. ② 关于Cookie中的setMaxAg ...

  3. Mysql 为什么不建议在 MySQL 中使用 UTF-8?

    最近我遇到了一个bug,我试着通过Rails在以“utf8”编码的MariaDB中保存一个UTF-8字符串,然后出现了一个离奇的错误: Incorrect string value: ‘😃 &l ...

  4. 2019.11.29 Mysql的数据操作

    为名为name的表增加数据(插入所有字段) insert into name values(1,‘张三’,‘男’,20); 为名为name的表增加数据(插入部分字段) insert into name ...

  5. 1-STM32+W5500+GPRS物联网开发基础篇-工控板简介

    最近这些日子都在忙活STM+W5500+GPRS的板子,所以前面的那块板子的教程耽搁了些时间. 这次的板子和上一版相比更贴近了使用,是因为有朋友督促我要做一块直接可以在工厂使用的板子,所以设计了这一块 ...

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

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

  7. PHP中使用 Memcached 的测试案例

    <?php class MemcacheController extends ControllerBase { public function indexAction() { session_s ...

  8. NTT小结及原根求法

    注意 由于蒟蒻实在太弱了~^_^~暂时无法完成证明,仅能写出简单版总结 与FFT的区别 \(NTT\)与\(FFT\)的代码区别就是把单位根换成了原根,从而实现无精度误差与浮点数的巨大常数 原根具有单 ...

  9. 【随记】Sql Server 2008 R2 备份时“无法打开备份设备”

    如下图所示,在执行SQL一个简单的备份命令时发生下面的错误 可能的原因: 1.文件夹权限问题: 2.Sql Server SQLServer服务器用户策略问题: 问题排查: 1.查看了temp文件夹, ...

  10. DM当中用文本输入点【转载】

    摘自<ANSYS 13.0 Workbench数值模拟技术> 通过XYZ坐标的文本文件创建3D曲线,文本需要满足一定的格式,格式化文本中,#表示此行是注释,忽略空行,数据行包括5个数据域, ...