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,用做差法节约时间

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 所有方法都要用的变量必须声明为成员变量,要仔细点别漏了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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 范围求和系列的更多相关文章

  1. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  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 ...

  3. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  4. LeetCode #303. Range Sum Query

    问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  5. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  6. 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 ...

  7. 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 ...

  8. 303. Range Sum Query - Immutable(动态规划)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. 【leetcode❤python】 303. Range Sum Query - Immutable

    #-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...

随机推荐

  1. RabbitMQ消息的消费与持久化

    作为消费者的客户端要消费Rabbitmq的消息,首先要建立与它某个队列的连接,具体连接时可指定队列的BindingKey和关系的exchange标识,Rabbitmq判断若已有队列通过BindingK ...

  2. Spring Cloud微服务框架介绍

    Spring Cloud为开发人员提供了一整套的快速构建分布式应用的工具,入服务注册.服务发现.熔断.负载均衡.路由等,提供了开箱即用的各种依赖以及良好的可扩展机制. 目前在Spring Cloud的 ...

  3. [LeetCode系列]链表环探测问题II

    给定一个链表头, 探测其是否有环, 如果没有返回NULL, 如果有返回环开始的位置. 环开始的位置定义为被两个指针指向的位置. 算法描述: 1. 快慢指针遍历, 如果到头说明无环返回NULL, 如果相 ...

  4. XaaS简介(关于IssS,PaaS以及SaaS)

    IaaS,比较容易理解,提供了一个操作系统以及操作系统的硬件支撑:阿里云: PaaS,提供了一个平台,或者说,使用PaaS是希望能够在上面建立自己的服务/应用,同时平台会提供一些API或者工具,能够降 ...

  5. Linux 制作补丁 打补丁 撤销补丁

    1.制作补丁 diff - 逐行比较文件 格式 diff   参数   旧文件/旧文件夹   新文件/新文件夹 -N   将不存在的文件看作是空的 -a   将所有文件都视为文本文件 -u   以合并 ...

  6. GOF23设计模式之桥接模式(bridge)

    一.桥接模式概述 桥接模式核心要点: 处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象层建立关联. 二.桥接模式场景提出与存在问题 商城系统中常见 ...

  7. java web 程序---登陆验证注销/重定向session_login.jsp/

    思路:第一个页面是:session_login.页面,一个form表单,一个脚本,输入的名称不为空,不,则重定向 到welcome.jsp页面.否则,显示登陆失败,请输入登陆名称: 第二个页面,是we ...

  8. 汇编_指令_LEA和MOV的区别

    就是目标地址传送指令: 将一个近地址指针写入到指定的寄存器.格式: LEA reg16,mem16 其中reg16必须是一个16位通用寄存器,mem16必须是一个存储器,执行这个指令后,就将mem16 ...

  9. SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  10. windows python文件拷贝到linux上执行问题-换行符问题/r/n

    之前在Windows下写好了一个Python脚本,运行没问题,今天在Linux下,脚本开头的注释行已经指明了解释器的路径,也用chmod给了执行权限,但就是不能直接运行脚本. 1 问题1: 报错:: ...