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
[暴力解法]:
时间分析:n
空间分析:n
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么节约时间、空间
[一句话思路]:
先存成preflixsum,用做差法节约时间
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 所有方法都要用的变量必须声明为成员变量,要仔细点别漏了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
先存成preflixsum,用做差法节约时间
[复杂度]:Time complexity: O(1) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
this关键字调用属性或者方法,数组存完了之后更新一下
(1)this调用本类中的属性,也就是类中的成员变量;
(2)this调用本类中的其他方法;
[关键模板化代码]:
省时间:
//store
int n = nums.length;
for (int i = 1; i < n; i++) {
nums[i] += nums[i - 1];
}
[其他解法]:
brute force
[Follow Up]:
[LC给出的题目变变变]:
307. Range Sum Query - Mutable 不是再加一次,而是换用线段树了。那我也没办法
304. Range Sum Query 2D - Immutable 二维就得要上dp了
[代码风格] :
class NumArray {
int[] nums;
public NumArray(int[] nums) {
//store
int n = nums.length;
for (int i = 1; i < n; i++) {
nums[i] += nums[i - 1];
}
//update
this.nums = nums;
}
public int sumRange(int i, int j) {
//corner case
if (i == 0) {
return nums[j];
}
//return
return nums[j] - nums[i - 1];
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
303. Range Sum Query 范围求和系列的更多相关文章
- [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 #303. Range Sum Query
问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 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
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 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❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
随机推荐
- 搭建一个IntelliJ的Spark项目
之前发现创建一个新项目之后,无法添加scala class 创建新项目 选择maven项目,然后选择simple或者quickstart: 进入项目后,在Project Structure里面,在gl ...
- bzoj 3671 [Noi2014]随机数生成器——贪心(时间复杂度分配)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3671 设 x 为一个点的行号, y 为一个点的列号:原本想着判断一个点能不能选就是看选了的点 ...
- Eclipse Failed to load D:\android-sdk-windows\build-tools\27.0.3\lib\dx.jar
Failed to load D:\android-sdk-windows\build-tools\27.0.3\lib\dx.jar Unknown error: Unable to build: ...
- RedHat6.5用ISO配置yum源
CentOS自带强大的yum功能,默认为从网上自动下载rpm包,对于网速不太给力或者没有网络的情况下需要用的话就不是很方便,很多软件尤其是服务器上的软件我们么有必要追求最新,稳定性最重要,这里我们用C ...
- OPCClient和OPCServer在Windows上运行方式的恩怨
http://www.diangon.com/wenku/PLC/201504/00021970.html 近段时间,遇到不少人都被OPCClient与OPCServer之间的通讯搞得头大,通过几次远 ...
- boost 编译 安装
首先到 boost.org 下载 boost_1_54_0.tar.gz 上传到 linux 环境下 解压缩 给解压缩出来的文件斌权限 chmod 777 ./* 执行己写好的 shell脚本 boo ...
- IT诗词
年少太轻狂,误入IT行.白发森森立,两眼直茫茫.语言数十种,无一称擅长.三十而立时,无房单身郎. 年少不经事,埋头编程忙. 指键铿锵落,不及细思量. bug千百个,comment无一行. 休言敏捷易, ...
- jmeter线程组 讲解
- GC之九--gc调优
目标 满足应用的响应时间和吞吐量需求,尽量减少GC对应用的影响 原则 大部分时候都不需要调优GC,只需配置-Xms,-Xmx即可,JVM会自动进行调整 先满足响应时间需求,再满足吞吐量需求 FullG ...
- error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
编译出现如下错误: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such ...