- 浏览: 173690 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目的意思是给定一个数组,如果调用sumRange(i, j) 就返回(i, j)之间 所有元素的和,题目Note中提到sumRange会被调用好多次,这句话的意思就是不希望我们每次都计算从i到j之间的和,这样会浪费很多时间。我们就需要先计算好每个区间的和,然后在调用sumRange后直接返回结果,不需要每次都计算。代码如下:
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.
题目的意思是给定一个数组,如果调用sumRange(i, j) 就返回(i, j)之间 所有元素的和,题目Note中提到sumRange会被调用好多次,这句话的意思就是不希望我们每次都计算从i到j之间的和,这样会浪费很多时间。我们就需要先计算好每个区间的和,然后在调用sumRange后直接返回结果,不需要每次都计算。代码如下:
public class NumArray { int[] sumArray; public NumArray(int[] nums) { sumArray = new int[nums.length + 1]; for(int i = 1; i <= nums.length; i++) { sumArray[i] = nums[i - 1] + sumArray[i - 1]; } } public int sumRange(int i, int j) { return sumArray[j + 1] - sumArray[i]; } } // Your NumArray object will be instantiated and called as such: // NumArray numArray = new NumArray(nums); // numArray.sumRange(0, 1); // numArray.sumRange(1, 2);
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 228You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 335Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 513Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 431Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 622Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 534Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 862Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
dot-prop-immutable, 点prop的不可变版本,带有一些扩展名 dot-prop-immutable 点prop的不可变版本,带有一些扩展名。npm install dot-prop-immutable这个模块的动机是在不改变普通JavaScript对象的现有状态的情况
前端开源库-immutable-core不变的核心,不变的模块化框架
前端开源库-immutable-ai不可变人工智能,不可变核心的可选接口
前端开源库-typed-immutable类型化的不可变、不可变的结构类型化数据
前端开源库-immutable-ai.zip
go-immutable-radix, 在Golang中,一个不可变的基数树实现 go-immutable-radix 提供实现不可变 radix的iradix 包。 包只提供单个 Tree 实现,针对稀疏节点优化。作为一个基数树,它提供以下内容:O(k) 操作。在许多...
前端项目-seamless-immutable,Javascript的不可变数据结构,与普通JS数组和对象向后兼容。
前端开源库-immutable-instance-id不可变实例ID,每个进程实例生成一次随机唯一ID
前端开源库-immutable-css不可变的css,不可变选择器的css linter。
前端大厂最新面试题-immutable.docx
前端开源库-immutable-require-valid-log-client不可变要求有效的日志客户端,不可变的日志客户端验证函数
前端开源库-immutable-require-valid-optional-object不可变要求有效的可选对象,如果参数不是对象,则返回空对象或引发异常
ToDo-react-redux-immutable, ToDo应用显示使用反应,重现和ImmutableJS的最佳实践 技巧,技巧和最佳实践,使用反应,重现和 ImmutableJS请检查文章解释逻辑> ...ToDo应用演示如何使用Red
Range Sum Query 2D - Immutable 图 Clone Graph 位操作 Reverse Bits Repeated DNA Sequences Number of 1 Bits Gray Code Single Number Single Number II Single Number III Power of Two Missing Number Maximum...
Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. Count Numbers with Unique Digits [357]12. 2-Key
CKS - Practise Immutable Resource
给定两个对象,获取它们之间的Seamless-immutable-diff 安装 如果尚未下载node,请从下载并安装。 npm install seamless-immutable-diff --save 用法 import diff from 'seamless-immutable-diff' ; import ...
article-json-immutable-methods 文章json的不可变方法安装如果尚未下载node,请从下载并安装。 npm install article-json-immutable-methods --save测验npm installnpm test依存关系 :通常可变数组方法的不可变...
npm install dot-prop-immutable 这个模块的动机是有一个简单的实用程序来改变 React-Redux 应用程序中的状态,而不会改变普通 JavaScript 对象的现有状态。 如果您想要真正的不可变数据集合,请查看酷炫的库 。 一...
npm install --save-dev chai-immutable yarn add --dev chai-immutable 然后,您可以将此插件与其他任何Chai插件一起使用: const chai = require ( 'chai' ) ; const chaiImmutable = require ( 'chai-immutable...