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) ...
随机推荐
- Eclipse的maven工具
左侧是组件以及组件依赖树(层级结构):右侧是识别出来的所有的组件:
- arm_linux QT+v4l 显示视频
1.参考(原创)基于ZedBoard的Webcam设计(三):视频的采集和动态显示 下载代码实测可用. 2.重新下载了csdn的代码,缺widget.h文件,后重新生成widget工程(自动产生wid ...
- centos6.6安装SVN服务器(2015/3/7)
一.安装#yum install subversion 判断是否安装成功 [root@]# svnserve --version有了SVN软件后还需要建立SVN库.#mkdir /opt/svn/ ...
- Linux环境安装配置Swftools
系统:CentOS6.5的64位版本 这里有一位仁兄的几个错误处理办法,下面是swftools的安装配置步骤: 1.安装所需的库和组件.机器之前安装过了,主要安装的是下面几个组件.如果不安装会 ...
- zk中文乱码问题
之前讲了怎么把数据导入到zookeeper(见zookeeper事件监听的importData方法),虽然本机win10的zookeeper展示没问题,但到了linux上就出现乱码了: << ...
- 通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- 修改配置文件matplotlibrc,让Matplotlib显示中文
matplotlib默认不支持中文显示,网上的解决办法有好多种,但是大多数都是通过在代码中指定字体,虽然也能实现,但是多出那么几行代码让人觉得很恶心. 本文介绍一种通过修改配置文件matplotlib ...
- Web API 路由访问设置
前段时间一直致力于MVC webapi 技术的研究,中途也遇到过好多阻碍,特别是api路由的设置和URL的访问形式,所以针对这个问题,特意做出了记录,以供日后有同样困惑的大虾们借鉴: 在Mvc WEB ...
- 1、Java通过JDBC操作Hive
0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hive也提供客户端的实现,通过HiveServer或者HiveServer2,客户端可以在不启动CL ...
- Java-Runoob:Java Stream、File、IO
ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...