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) ...
随机推荐
- RK3288 摄像头左右镜像
系统:Android 5.1 设置摄像头左右镜像 diff --git a/frameworks/av/services/camera/libcameraservice/api1/CameraClie ...
- java.lang.String.trim(), 不仅仅去掉空格
由于我们处理的日志需要过滤一些空格,因此大部分处理日志的程序中都用到了java.lang.String.trim()函数.直到有一次遇到一个诡异的问题,某个包含特殊字符的字符串被trim后居然也为 ...
- POJ1159解题心得
题目:http://poj.org/problem?id=1159 刚开始,从样例的特征去思考.总让我从回文数的角度去思考,想出几个方案,可都用了数据去检验,发现不行.如:ABCDDCB,BACDCA ...
- 使用Eclipse可以启动服务器,却不能访问localhost
今天心血来潮修改了Tomcat的端口号,将默认的8080改为8888,使用MyEclipse部署项目没有问题,只是访问的地址不可以使用8080而是要用8888,这是当然的了,毕竟我修改了.但是使用Ec ...
- netcore中使用log4net日志
第一.控制台程序中使用log4net static void Main(string[] args) { ILoggerRepository repository = LoggerManager.C ...
- 脱壳系列(二) - EZIP 壳
程序: 运行程序 用 PEiD 查壳 EZIP 1.0 用 OD 打开 按 F8 往下走 这个看似是 OEP 的地方却不是 OEP 因为代码段从 00401000 开始 可以看到,壳伪造了 3 个区段 ...
- php header 302重定向失效问题
E:\html\pim\php_weili_activities\application\controllers\user.php public function login() { if ($thi ...
- 通过Java代码装配Bean
上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...
- 基于vue-easytable实现数据的增删改查
基于vue-easytable实现数据的增删改查 原理:利用vue的数据绑定和vue-easetable的ui完成增删改查 后端接口: 1.条件查询表中数据 http://localhost:4795 ...
- Android Studio 2.3.3 调用asp.net webService实战(拒绝忽悠)
1.路径中不能包含localhost(本来想在本机调试,就是不行,没办法发布到远程服务器) 2.必须采用异步的办法(阻塞主线程的是肯定不行了) 3.以下是全部的源代码(毫不保留) package co ...