Leetcode 303 Range Sum Query
来源:互联网 发布:啊哈 算法 epub 编辑:程序博客网 时间:2024/06/05 16:21
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) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3
Note:
- You may assume that the array does not change.
- There are many calls to sumRange function.
基本方法可以做,但是time 超了= 。=
考虑在复制的时候进行累加和计算。
public class NumArray { int[] array;public NumArray(int[] nums) { for(int i = 1; i < nums.length; i++) nums[i] += nums[i - 1]; array = nums;}public int sumRange(int i, int j) { if(i == 0) return array[j]; return array[j] - array[i - 1];}}/** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */
阅读全文
0 0
- Leetcode 303 Range Sum Query
- Leetcode 303 Range Sum Query
- LeetCode 303 Range Sum Query
- [Leetcode]Range Sum Query
- Range Sum Query(leetcode)
- [LeetCode]Range Sum Query
- leetcode---Range Sum Query
- LeetCode : Range Sum Query
- [LeetCode] Range Sum Query
- leetCode---Range Sum Query
- leetcode[Range Sum Query
- LeetCode -- Range Sum Query
- [leetcode-303]Range Sum Query - Immutable
- [leetcode-303]Range Sum Query - Immutable(java)
- leetcode 303: Range Sum Query - Immutable
- Range Sum Query - Immutable leetcode 303
- leetcode 303 : Range Sum Query - Immutable
- LeetCode 303 : Range Sum Query - Immutable (Java)
- JDBC的学习 一
- BZOJ 3295: [Cqoi2011]动态逆序对 分块大法好
- 港股通收市竞价交易机制科普
- jdbc介绍
- 刷题--顺时针打印矩阵
- Leetcode 303 Range Sum Query
- 机房重构——全局变量
- rel、href、src、url的区别
- Size_t
- day1
- Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree (树上尼姆博弈)
- Error:Content is not allowed in prolog.
- 【LeetCode】448.Find All Numbers Disappeared in an Array
- Spring-AOP