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. filter异常捕捉

    jsp中抛出一个异常 <% String action = request.getParameter("action"); if ("accountExceptio ...

  2. SecureCRT导入已有会话

    如果别人有完整的环境信息,我们想拿过来,怎么导入?或者别人想要我的会话配置信息,怎么导出?对SecureCRT这个工具来说很easy,根本不需要去找什么导入.导出按钮,直接文件操作. 假如我的Secu ...

  3. Android JNI中的数据传递

    1.JNI 基本类型 当 Java 代码与本地代码 C/C++ 代码相互调用时,肯定会有参数的传递.两者属于不同的语言,数据类型有差别,此时,JNI 要保证两种语言之间的数据类型和数据空间大小的匹配. ...

  4. Apache DBUtils使用总结 【转】

    Apache DBUtils使用总结   DBUtils是个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果集封装成JavaBean,这就为我们做了最枯燥乏味. ...

  5. Linux和Windows的遍历目录下所有文件的方法对比

    首先两者读取所有文件的方法都是采用迭代的方式,首先用函数A的返回值判断目录下是否有文件,然后返回值合法则在循环中用函数B直到函数B的返回值不合法为止.最后用函数C释放资源. 1.打开目录 #inclu ...

  6. 1、Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunmen ...

  7. Windows2012使用笔记

    一.介绍 win 2012的名字于北京时间2012年4月18日公布,全称Windows Server 2012(下面简称win 2012),正式版于2012年9月4日发布.这是一套基于Windows ...

  8. ubuntu16.04让内核编译一次过的方法

    问题: 进入内核后,发现make menuconfig 出错,而且在在网上找到的一些安装包,安装结束后,发现make menuconfig后的图形界面虽然出来了,但是图形界面里的内容没有出来! 解决方 ...

  9. 【UVA】10763 Foreign Exchange(map)

    题目 题目     分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢.     代码 #include < ...

  10. iOS开发API常用英语名词

    iOS开发API常用英语名词 0. indicating 决定 1.in order to 以便 2.rectangle bounds 矩形尺寸 3.applied 应用 4.entirety 全部 ...