LeetCode:区域和检索【303】
LeetCode:区域和检索【303】
题目描述
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
示例:
给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange() sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
说明:
- 你可以假设数组不可变。
- 会多次调用 sumRange 方法。
题目分析
sums[i] = nums[0] + nums[1] + … + nums[i]
sumRange(i, j) = sums[j] – sums[i – 1]
Time complexity: pre-compute: O(n), query: O(1)
Space complexity: O(n)
Java题解
class NumArray {
private int[] sums =null;
public NumArray(int[] nums) {
if(nums.length==0)
return;
sums = new int[nums.length];
sums[0]=nums[0];
for(int i=1;i<nums.length;i++)
sums[i]=nums[i]+sums[i-1];
}
public int sumRange(int i, int j) {
if(i==0)
return sums[j];
return sums[j]-sums[i-1];
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
LeetCode:区域和检索【303】的更多相关文章
- [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 ...
- Java实现 LeetCode 303 区域和检索 - 数组不可变
303. 区域和检索 - 数组不可变 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, ...
- [Leetcode]303.区域和检索&&304.二维区域和检索
题目 1.区域和检索: 简单题,前缀和方法 乍一看就觉得应该用前缀和来做,一个数组多次查询. 实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和, 需要找 ...
- [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- Leetcode 304.二维区域和检索-矩阵不可变
二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...
- Java实现 LeetCode 307 区域和检索 - 数组可修改
307. 区域和检索 - 数组可修改 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. update(i, val) 函数可以通过将下标 ...
- Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变
304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...
随机推荐
- Jenkins Docker 插件
原文地址:https://wiki.jenkins.io/display/JENKINS/Docker+Plugin Created by magnayn -, last modified by Ni ...
- c# 冒号 :
1.表示继承关系 class classA:classB 2.继承构造, 调用自己这个类的某个构造函数,因为有的类构造函数很多参数,而有的参数又不是必须填写,或者可以提供一些默认值,就跟重载是一样的道 ...
- python--pipe
1.pipe 除了使用队列外,还可以使用管道在进程间执行消息传递 pipe([]duplex) 在进程间创建一条管道,并返回元组(conn1,conn2),其中conn1和conn2是表示管道两端的C ...
- HTMLTestRunner 异常输出中文乱码
1.在代码中加入下面的代码并保存: # -.- coding:utf-8 -.- import sys reload(sys) sys.setdefaultencoding('utf-8') 2.找到 ...
- 使Gallery时设置居左显示
Gallery中的图片默认是居中显示的.可是在非常多情况下我们须要它居左显示,这样做有一个简单方法.就是把Gallery的left设置为负多少,如以下的方法: Drawable drawable=ca ...
- Python内置函数之range()
range(stop)range(start,stop[,step]) 返回一个range对象,第三个参数的含义为:间隔的个数. range对象同时也是可迭代对象. >>> isin ...
- CentOS7 安装 Node.js
1.首先安装node.js 的版本管理工具 NVM,执行以下命令: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/ ...
- (转) 对svn分支合并类型和深度的理解
合并的工作是把主干或者分支上合并范围内的所有改动列出,并对比当前工作副本的内容,由合并者手工修改冲突,然后提交到服务器的相应目录里.如果当前工作副本是主干,则合并的范围是分支上的改动,如果工作副本是分 ...
- CSMA/CD解释与理解
1. CSMA/CD含义 CSMA/CD即载波监听多点接入/碰撞检测,此协议是使用在总线型网络中的,不同计算机是通过多点接入的方式连接在一起.协议的重点在于监听和碰撞检测. 2. 为什么要监听和碰撞检 ...
- Selenium+Python :WebDriver设计模式( Page Object )
Page Object 设计原理 Page Object设计模式是Selenium自动化测试项目的最佳设计模式之一,强调测试.逻辑.数据和驱动相互分离. Page Object模式是Selenium中 ...