题目

303. Range Sum Query - Immutable

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

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

Constraints:

You may assume that the array does not change.

There are many calls to sumRange function.

1 穷举解法

发现速度很慢,因为约束说了,这是要重复使用的。只跑赢了5%的提交。

public class NumArray {private int[] nums;public NumArray(int[] nums) {this.nums = nums;}public int sumRange(int i, int j) {int sum = 0;for (; i <= j && j < nums.length; i++) {sum += nums[i];}return sum;}}

2 Cache的方式解法

因为题目强调了,sumRange方法需要被多次调用,那么一定要有缓存,才能达到O(1)的速度。

这里有个公式sums[i + 1] = sums[i] + nums[i]可以把前面多少个数的和缓存起来,求区间数据只要用return sums[j + 1] - sums[i]; 就可以O(1)算出来。

public class NumArray {private int[] sums;public NumArray(int[] nums) {sums = new int[nums.length + 1];for (int i = 0; i < nums.length; i++) {sums[i + 1] = sums[i] + nums[i];}}public int sumRange(int i, int j) {return sums[j + 1] - sums[i];}}

算法:求数组区间的和Range Sum Query - Immutable相关推荐

  1. leetcode 303. Range Sum Query - Immutable | 303. 区域和检索 - 数组不可变(一维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-immutable/ 题解 标准的前缀和问题,简单题,不多说,直接上代码 import java.ut ...

  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), inclu ...

  3. 重拾编程之路--jeetcode(java)--Range Sum Query - Immutable

    算法理解:         1)用一个和数组记录前几个元素的和:               sum[0]---(0,0)的和:               sum[1]-- (0,1)的和:     ...

  4. Leetcode题目:Range Sum Query - Immutable

    题目: 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

    https://leetcode.com/problems/range-sum-query-immutable/ 用一个 sum 数组,sum[i] -- nums 中选出前 i 个元素,求和所得到的 ...

  6. 303. Range Sum Query - Immutable

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

  7. LeetCode Range Sum Query Immutable

    2131231 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10873807.html

  8. leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...

  9. 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...

  10. java打印数组的连续非空子集_Apriori算法求数组的非空子集java代码

    Apriori算法求集合的非空子集java代码 public class Test { public static void main(String[] args) { String str=&quo ...

最新文章

  1. springcloud项目打包_SpringCloud 快速入门
  2. 拼多多一度跌破发行价:已遭上海工商约谈 被要求自查自纠
  3. oracle 常用命令大汇总
  4. 仟叶学校:武汉老师最燃演讲“人生很贵,请别浪费”
  5. javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)...
  6. python赋值、深浅拷贝
  7. 2021年东港二中高考成绩查询,辽宁省东港二中2021届英语高考模拟试卷1(新课改原创2020版,供2021年课改省份考生使用)...
  8. as cast float server sql_Sql Server中Float格式转换字符串varchar方法(转)
  9. 利用数据库创建webservice
  10. python zip函数转制_Python zip函数及用法
  11. mybatis处理集合和循环数组问题
  12. 六月总结-工作一年总结
  13. 【转】 Linux中的工作队列
  14. 数据库课设 - 机票预订系统
  15. php检查gd库是否开启,检查服务器是否开启GD库
  16. 翻译软件免费版下载-免费版翻译软件下载
  17. 中国城市燃气行业供需前景及未来竞争走势研究报告2021版
  18. php 模拟蜘蛛,PHP实现使用CURL模拟百度蜘蛛进行采集
  19. 英语心理测试脸型软件,心理测试:脸型分析自己
  20. with admin option和with grant option的区别与用法

热门文章

  1. python设计模式pdf_精通python设计模式豆瓣-精通python设计模式第二版电子书pdf下载-精品下载...
  2. html做换装游戏,index.html
  3. 理解position:relative 与 position:absolute
  4. PHP面试常考内容之面向对象(2)
  5. 路由器修改hosts实现域名劫持
  6. Effective_STL 学习笔记(十六) 如何将 vector 和 string 的数据传给遗留的API
  7. java中使用阻塞队列实现生产这与消费这之间的关系
  8. Socket Tcp高密集信息广播转发强度测试
  9. Model to Text工具Acceleo使用教程(七)——模板服务
  10. 简述物联网感知技术_雄芯一号芯片发布 智慧城市物联网感知体系升级换新