Immutable

[抄题]:

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

Example:

  1. Given nums = [-2, 0, 3, -5, 2, -1]
  2.  
  3. sumRange(0, 2) -> 1
  4. sumRange(2, 5) -> -1
  5. sumRange(0, 5) -> -3

[暴力解法]:

时间分析:n

空间分析:n

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么节约时间、空间

[一句话思路]:

先存成preflixsum,用做差法节约时间

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

[画图]:

[一刷]:

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

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

先存成preflixsum,用做差法节约时间

[复杂度]:Time complexity: O(1) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

this关键字调用属性或者方法,数组存完了之后更新一下

  1. (1)this调用本类中的属性,也就是类中的成员变量;
  2. (2)this调用本类中的其他方法;

[关键模板化代码]:

省时间:

  1. //store
  2. int n = nums.length;
  3. for (int i = 1; i < n; i++) {
  4. nums[i] += nums[i - 1];
  5. }

[其他解法]:

brute force

[Follow Up]:

[LC给出的题目变变变]:

307. Range Sum Query - Mutable 不是再加一次,而是换用线段树了。那我也没办法

304. Range Sum Query 2D - Immutable 二维就得要上dp了

[代码风格] :

  1. class NumArray {
  2.  
  3. int[] nums;
  4.  
  5. public NumArray(int[] nums) {
  6. //store
  7. int n = nums.length;
  8. for (int i = 1; i < n; i++) {
  9. nums[i] += nums[i - 1];
  10. }
  11. //update
  12. this.nums = nums;
  13. }
  14.  
  15. public int sumRange(int i, int j) {
  16. //corner case
  17. if (i == 0) {
  18. return nums[j];
  19. }
  20. //return
  21. return nums[j] - nums[i - 1];
  22. }
  23. }
  24.  
  25. /**
  26. * Your NumArray object will be instantiated and called as such:
  27. * NumArray obj = new NumArray(nums);
  28. * int param_1 = obj.sumRange(i,j);
  29. */

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权限识别

    由于书上的例子弄不出来 自己瞎弄了个简易版的 登陆页面 <%@ page language="java" import="java.util.*" pag ...

  2. (转)Android性能优化——工具篇

    Android性能优化是Android开发中经常遇见的一个问题,接下来将对Android性能优化方面的知识点做一个简单的梳理和总结,将从工具和代码两方面进行梳理.所谓工欲善其事必先利其器,本文首先来看 ...

  3. 16.Python使用lxml爬虫

    1.lxml是解析库,使用时需要导入该包,直接在命令行输入:pip3 install lxml,基本上会报错.正确应该去对应的网址:https://pypi.org/project/lxml/#fil ...

  4. 页面加载完之后在执行js代码

    把代码写在 window.onload = function () { //js代码 //此处js代码是页面完全加载完之后执行 } 即可. 例: <script type="text/ ...

  5. Spring AOP表达式报错:Pointcut is not well-formed: expecting 'name pattern' at character position

    问题现象: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test ...

  6. 理解加密算法——创建CA机构,签发证书并开始TLS通信

    1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下,NODE.JS代码: TCP Server: const net=requir ...

  7. 黄聪:使用Add-on SDK开发火狐扩展

    2014年3月7号更新:火狐已经关闭Add-on SDK服务啦!我也转用360急速浏览器来开发浏览器插件了. 如果精通JS.HTML.CSS开发的朋友,直接看这个教程估计就能懂了 ---------- ...

  8. nginx 作为反向代理实现负载均衡的例子

    以下我们就来举例说明如何使用 nginx 实现负载均衡.因为nginx在处理并发方面的优势,现在这个应用非常常见.nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 〉直接作 ...

  9. Nmon、nmon analyse安装及使用

    性能监控算是性能测试中的一部分,测试人员需要去分析各类系统指标,CPU.网络.内存.磁盘I/O等等.嗯.通常linux系统下有诸如top.netstat.iostat等命令进行查看:而有时需要看某数据 ...

  10. BIOS设置图解教程-看完就没有不明白的了

    BIOS(基本输入/输出系统)是被固化在计算机CMOS RAM芯片中的一组程序,为计算机提供最初的.最直接的硬件控制.BIOS主要有两类∶AWARD BIOS和AMI BIOS.正确设置BIOS可大大 ...