[LeetCode] 303. Range Sum Query - Immutable
https://leetcode.com/problems/range-sum-query-immutable/
用一个 sum 数组,sum[i] -- nums 中选出前 i 个元素,求和所得到的值。
这样就有:
nums = [-2, 0, 3, -5, 2, -1] sum = [0, -2, -2, 1, -4, -2, -3] sumRange(0, 2) -> sum[2 + 1] - sum[0] -> 1 sumRange(2, 5) -> sum[5 + 1] - sum[2] -> -1 sumRange(0, 5) -> sum[5 + 1] - sum[0] -> -3
public class NumArray {int[] sum;public NumArray(int[] nums) {sum = new int[nums.length + 1];for (int i = 0; i < nums.length; i++) {sum[i + 1] = sum[i] + nums[i]; } } public int sumRange(int i, int j) { return sum[j + 1] - sum[i]; } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */
转载于:https://www.cnblogs.com/chencode/p/range-sum-query-immutable.html
[LeetCode] 303. Range Sum Query - Immutable相关推荐
- leetcode 303. Range Sum Query - Immutable | 303. 区域和检索 - 数组不可变(一维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-immutable/ 题解 标准的前缀和问题,简单题,不多说,直接上代码 import java.ut ...
- 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 ...
- 303. Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- Leetcode题目:Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- [Leetcode] 304. Range Sum Query 2D - Immutable 解题报告
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- LeetCode Range Sum Query Immutable
2131231 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10873807.html
- 重拾编程之路--jeetcode(java)--Range Sum Query - Immutable
算法理解: 1)用一个和数组记录前几个元素的和: sum[0]---(0,0)的和: sum[1]-- (0,1)的和: ...
最新文章
- WebStorm配置SVN
- sqlserver中无ldf文件附加数据库
- 台式计算机不用待机还是关机好,电脑应该选择“关机”还是“休眠”?不关机对电脑有损伤吗?...
- a标签隐藏真实地址_家庭影院布线非常杂乱应该怎么补救?A/V电缆管理的7个技巧...
- oracle 打开 ctl,Oracle 19c 随系统systemctl启动数据库
- Java字符流的使用
- 循序渐进DB2 (第2版)——DBA系统管理、运维与应用案例
- 模板元实现顺序、分支和循环结构
- Js toString()方法笔记
- iOS TableView性能优化
- 安卓手机实现wifi中继、WiFi信号增强
- android动画素材下载,Android Lottie动画使用
- 热度php代码,爬取知乎热度搜索标题并数据分析及可视化(示例代码)
- 数字化名词解释—数字化转型
- IOC 回顾 | 踏扁舟一叶 逐野鹤 行广阔
- 应用程序正常初始化(0xc0000034)失败
- python数据分析002—python基础语法
- 社交APP的核心功能都有哪些
- Carson带你学Android:图文详解RxJava背压策略
- 光模块自动测试系统软件,一种用于测试光模块的多通道自动测试方法及系统