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. 模拟赛20181031 雅礼 Wearry 养花 折射 画作

    % Day1 Solution % Wearry % Stay determined! 养花    考虑当 kkk 确定的时候如何求答案, 显然对于所有形如 [ak,(a+1)k)[ak, (a+1) ...

  2. [Flutter] Router Navigation

    Basic navigation by using 'Navigator.push' & 'Navigator.pop()', for example, we have two screen, ...

  3. php自定义函数之参数的引用

    在变量这个函数中,我们学习了变量的引用,我们来回顾一下知识:大理石平台支架 <?php $a = 10; $b = &$a; $a = 100; echo $a.'---------'. ...

  4. 关灯问题II 状压DP

    关灯问题II 状压DP \(n\)个灯,\(m\)个按钮,每个按钮都会对每个灯有不同影响,问最少多少次使灯熄完. \(n\le 10,m\le 100\) 状压DP的好题,体现了状压的基本套路与二进制 ...

  5. 【概率论】6-1:大样本介绍(Large Random Samples Introduction)

    title: [概率论]6-1:大样本介绍(Large Random Samples Introduction) categories: - Mathematic - Probability keyw ...

  6. (28)打鸡儿教你Vue.js

    单件商品金额计算和单选全选功能 new Vue({ el: '#app', data: { totalMoney: 0, productList: [] }, filters: { formatMon ...

  7. 2D到3D视频转换 三维重建

    2D到3D视频转换(也称为2D到立体3D转换和立体转换)是将2D(“平面”)胶片转换为3D形式的过程,几乎在所有情况下都是立体声,因此它是创建图像的过程.每个眼睛来自一个2D图像. 内容 1概述 1. ...

  8. UOJ#470. 【ZJOI2019】语言 虚树,线段树合并

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ470.html 前言 做完情报中心来看这个题突然发现两题有相似之处然后就会做了. 题解 首先,我们考虑将所有答案点对分为两 ...

  9. bootstraptable重新加载

    bootstraptable需要彻底重新加载,发现即使url等参数更新过来仍旧无效.需要调用 $("#resource_table").bootstrapTable('destro ...

  10. select和C标签

    <select name="cpcyModel.rwzj" id="cpcyModel_rwzj"> <option value=" ...