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), 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 {
int[] nums; public NumArray(int[] nums) {
for(int i = 1; i < nums.length; i++){
nums[i] += nums[i - 1];
}
this.nums = nums;
} public int sumRange(int i, int j) {
if(i == 0)
return nums[j];
return nums[j] - nums[i - 1];
}
} // Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
Java [Leetcode 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
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
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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 ...
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...
- 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】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- Recommender Systems移动互联网个性化游戏推荐
对于在线商店,主要关心两方面:1. 提升转化率(将不消费的用户转变为消费用户):2. 提升消费额(已经花钱的人,花更多的强). 对比了6种方法:1. 协同过滤:2. slope one:3. 基于内容 ...
- Nagios 安装及微信短信提醒
引言 Nagios 作为业界非常强大的一款开源监视系统. 监控网络服务(SMTP.POP3.HTTP.NNTP.PING 等): 监控主机资源(处理器负荷.磁盘利用率等): 简单地插件设计使得用户可以 ...
- recursion lead to out of memory
There are two storage areas involved: the stack and the heap. The stack is where the current state o ...
- Android 用Activity的onTouchEvent来监听滑动手势
package com.example.activityOnTouchEvent; import android.app.Activity; import android.os.Bundle; imp ...
- Session过期,跳出iframe等框架
//在你想控制跳转的页面,如login.jsp中的<head>与</head>之间加入以下代码: if(window != top){ //解决Sessio ...
- BZOJ 3203 sdoi 2013 保护出题人
由于样例解释很清晰,所以很容易得到以下结论: 1.每一关都是独立的,且僵尸的相对位置不会变 2.每一关的攻击力=Max(sum(i)/dis(i)) 其实sum(i)是僵尸攻击力的前缀和,dis(i) ...
- POJ1248 Safecracker
第一次写DFS的程序,虽然是个水题.1. 学了memset2. 可以存下来A-Z的各个次方的结果3. 可以排序优化4. 我用了t[0]==0来判断是否有解,也可设个flag5. 用了递归,也可用五层循 ...
- pymongo 例子
import pymongo class dbUtil(object): def __init__(self, tablename='functional_testing'): con = pymon ...
- jquery通过ajax-json访问java后台传递参数,通过request.getParameter获取不到参数的说明
http://m.blog.csdn.net/blog/eyebrother/36007145 所以当后台通过request.getParameter("name");对参数值的作 ...
- 使用phantomjs对页面进行截图
本文章参考了使用phantomjs操作DOM并对页面进行截图需要注意的几个问题 及 phantomjs使用说明 这两篇文章,初次接触phantomjs的童鞋可以去看下这两篇原文 在学习中可以看下 ph ...