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.
public class NumArray {
private static int[] nums;
private static int[] sums;
public NumArray(int[] nums) {
if(nums==null)
return ;
this.nums = nums;
sums=new int[nums.length];//注意为什么不nums=sums,这样的话跟着变,这样的话原始数组也会变,这样不科学
if (nums.length == 1) {
sums[0] = nums[0];
return;
}
for (int x = 1; x < nums.length; x++) {
sums[x] = sums[x - 1] + nums[x];
}
}
public int sumRange(int i, int j) {
return sums[j]-sums[i]+nums[i];
}
}
// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
303. Range Sum Query - Immutable的更多相关文章
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 ...
- 【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),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- Java [Leetcode 303]Range Sum Query - Immutable
题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 303. 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 (C++)
题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...
随机推荐
- 在Eclipse中使用JUnit4进行单元测试
在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)
- HTTP API 设计指南(中文版) restfull
http://www.css88.com/archives/5121 目录 基础 总是使用TLS 在Accepts头中带上版本号 通过Etags支持缓存 用Request-Ids追踪请求 用Range ...
- 转(linux shell)
请把如下字符串 stu494 e222f stu495 bedf3 stu496 92236 stu497 49b91 转为如下形式: stu494=e222f stu495=bedf3 stu496 ...
- Android 网络编程--URL互联网资源
1.加入权限 <uses-permission android:name="android.permission.INTERNET"/> 2.Layout设计 < ...
- 06 Linux下Shell介绍
一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ...
- 解决方案:将已存在的项目 添加到 tfs解决方案中的时候 出现:新项目不能成功加入源码控制
遇到此问题 可能是因为你的 解决方案文件 没有正确与 tfs服务器绑定导致的 解决方式是: 在打开任意一个源码文件的时候,打开 vs2013的 文件>> Go to File->So ...
- 【转】JDBC为什么要使用PreparedStatement而不是Statement
http://www.importnew.com/5006.html PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedSta ...
- MIME对应表
文件后缀与MIME类型的对应表 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 3D视觉差---原生js+css
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...