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 ...
随机推荐
- go语言-二进制与位运算
一.进制介绍 1.二进制:0,1 -->不能直接用二进制来表示一个整数,用%b输出二进制 package mainimport "fmt"func main() { var ...
- LightOJ - 1297 - Largest Box(数学)
链接: https://vjudge.net/problem/LightOJ-1297 题意: In the following figure you can see a rectangular ca ...
- 配置asgi来达到能处理websocket
在项目中使用了webscoket进行实时通讯,但是生产环境又使用了django+nginx+uwsgi的部署方式,我们都知道uwsgi并不能处理websocket请求,所以需要asgi服务器来处理we ...
- dict 的 items() 方法与 iteritems() 方法的不同?
items方法将所有的字典以列表方式返回,其中项在返回时没有特殊的顺序: iteritems方法有相似的作用,但是返回一个迭代器对象
- 多个List 或 Array 进行 合并
1.调用 var aaa = new List<string>() { "0" }; var a1 = new List<string>() { " ...
- crontab 定时访问指定url,定时脚本
一.contab格式说明 二.定时访问url: 1.连接远程主机,连接成功后,输入命令 crontab -e : 2.参照VI编辑器.按字母 i 进入编辑模式,输入需要执行的脚本:(在这里之前要检查 ...
- Anaconda3自带jupyter
1.cmd命令行中输入 JupyterNotebook 2.系统自动调起下面页面(注册端口冲突是打不开的)
- python中普通函数调用协程
import asyncio def target(loop, timeout=None): future = asyncio.run_coroutine_threadsafe(add(1, b=2) ...
- vim 文本替换讲解
在VIM中进行文本替换: 1. 替换当前行中的内容: :s/from/to/ (s即substitude) :s/from/to/ : 将当前行中的第一个from,替换成to.如果当前行含有多个 fr ...
- Perl看完这个,再不敢说自己会玩贪吃蛇
某天闲逛时看见一副动图: 真的是非常贪吃,各种拐弯各种吃,感觉十分有趣. 用Perl来实现自动吃满,蓄谋已久,之前的字符贪吃蛇.深度优先算法.A*算法,都是为此篇做铺垫. 那么,怎样让蛇不吃到自己呢? ...