LeetCode_303. Range Sum Query - Immutable
303. Range Sum Query - Immutable
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.
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的更多相关文章
- [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 ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- leetcode303 Range Sum Query - Immutable
""" Given an integer array nums, find the sum of the elements between indices i and j ...
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- [LeetCode] Range Sum Query - Immutable
The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...
随机推荐
- tensorflow API _ 1 (control_flow_ops.cond)
该函数用来控制程序执行流,相当于if-else了import tensorflow as tffrom tensorflow.python.ops import control_flow_ops a ...
- Dobbox
一.向本地仓库导入Dubbox依赖 1.1解压压缩包 1.2打开cmd窗口切到源码包路径 1.3输入命令行 1.4成功后展示如图 1.5输入命令行 1.6成功后如图 public class DoSo ...
- Default Trace 查找日志文件快速增长的原因
select loginname, loginsid, spid, hostname, applicationname, servername, databasename, objectname, e ...
- c# 异步方法中HttpContext.Current为空
调用异步方法前 HttpContext context = System.Web.HttpContext.Current; HttpRuntime.Cache.Insert("context ...
- Python学习之--用户输入以及运算
一. 用户输入:input 1. 函数input() 让程序暂停运行,等待用户输入一些文本. 2.使用int()将字符串转为数值 二.运算:+-*. 1.求模运算符 (%)将两个数相除并返回余数: 2 ...
- Comet OJ - Contest #11题解
传送门 \(A\) 咕咕咕 const int N=1e6+5; char s[N],t[N];int n,res; inline bool cmp(const int &x,const in ...
- mysql group by order by havaing where 顺序
结论: select xx from xx where xx group by xx order by xxx; select xx from xx group by xx having xx ord ...
- 数据结构实验之排序三:bucket sort (SDUT 3400)
桶排序: #include <stdio.h> #include <string.h> int a[5555555]; int main() { int n,m; scanf( ...
- 转载:scala中的implicit
掌握implicit的用法是阅读Spark源码的基础,也是学习Scala其它的开源框架的关键,implicit 可分为: 隐式参数 隐式转换类型 隐式调用函数 1.隐式参数 当我们在定义方法时,可以把 ...
- C++多重继承分析——《虚继承实现原理(虚继承和虚函数)》
博客转载:https://blog.csdn.net/longlovefilm/article/details/80558879 一.虚继承和虚函数概念区分 虚继承和虚函数是完全无相关的两个概念. 虚 ...