[LC] 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.
Solution 1:
class NumArray { int[] prefix;
public NumArray(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
prefix = new int[nums.length];
prefix[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
prefix[i] = prefix[i - 1] + nums[i];
}
} public int sumRange(int i, int j) {
if (i == 0) {
return prefix[j];
}
return prefix[j] - prefix[i - 1];
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
Solution 2:
class NumArray {
int[] prefix;
public NumArray(int[] nums) {
prefix = new int[nums.length + 1];
for (int i = 0; i < nums.length; i++) {
prefix[i + 1] = prefix[i] + nums[i];
}
} public int sumRange(int i, int j) {
return prefix[j + 1] - prefix[i];
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
[LC] 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),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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]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 ...
随机推荐
- 第 36 章 TCP/IP协议基础
问题一:为什么要有缓存表?为什么表项要有过期时间而不是一直有效 1.参考网址: 1)网络——ARP协议 2)linux arp机制解析 2.解答: 2.1 ARP缓存可以减小广播量,当主机发送一个AR ...
- Python爬虫连载2-reponse\parse简介
一.reponse解析 urlopen的返回对象 (1)geturl:返回网页地址 (2)info:请求反馈对象的meta信息 (3)getcode:返回的http code from urllib ...
- 生成随机数(Random类)和获取用户输入(Scanner类)
生成指定范围内的随机数 Math.random() 生成随机数,随机数在0到1之间,类型是 double. public class randCase { public static void mai ...
- shiro的小白学习
1. shiro是啥就不用说了吧 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理 SecurityManager 是shiro的核心.它不同于java. ...
- vue实现简单的过滤器
html片段: <script src="https://unpkg.com/vue"></script> <div id="app&quo ...
- geodjango七日学习笔记 (7.30整理本地笔记上传到网络)
第一天进行到现在,在开端的尾巴,想起来写一个学习笔记, 开发环境已搭好,用的是pycharm 环境是本机已有的interpreter python3.7 接下来要做的是新建一个geodjango项 ...
- 面向对象 part4 构造函数对象重写
出处 其中深奥之处非看一次能了解 !对象真的有点绕,但是又很严谨
- 题解-------CF1304E 1-Trees and Queries
传送门 题目大意 给你一棵无根树,然后询问Q次,每次把点$x$和点$y$连接,问你从点$a$到点$b$是否有一条长度为$k$的简单路径,每次询问完后会把新添加的边删除. 思路:树上LCA 题目跟201 ...
- Java的各类型数据在内存中分配情况详解
1. 有这样一种说法,如今争锋于IT战场的两大势力,MS一族偏重于底层实现,Java一族偏重于系统架构.说法根据无从考证,但从两大势力各自的社区力量和图书市场已有佳作不难看出,此说法不虚,但 ...
- LFW Face Database下载
http://vis-www.cs.umass.edu/lfw/ Download the database: All images as gzipped tar file (173MB, md5su ...