当前位置:网站首页>前缀和数组系列
前缀和数组系列
2022-07-06 06:42:00 【王六六的IT日常】
涉及三道题:
303. 区域和检索 - 数组不可变
304. 二维区域和检索 - 矩阵不可变
560. 和为 K 的子数组
如果要得到「区间和」,能想到最简单的方法就是遍历所求区间,循环相加即可。如果这种需求有很多,此时,时间复杂度为 O(n^2)
基于上面描述的场景,我们完全可以使用「前缀和」优化,前缀和数组中每个元素的值为区间[0…i]的元素和。
注意:
前缀和适用于不变数组;对于变化的数组,可以使用「线段树」,关于线段树的详细介绍可见 线段树详解
区域和检索 - 数组不可变
区域和检索 - 数组不可变
题目详情可见 :区域和检索 - 数组不可变
建议:preSum[]整体向后偏移一位,简便处理
如果求区间[2,4]的和,只需计算preSum[4 + 1] - preSum[2]即可
代码:
class NumArray {
// 记录前缀和的数组
private int[] preSum;
public NumArray(int[] nums) {
// preSum 从 1 开始,避免越界问题
preSum = new int[nums.length + 1];
for (int i = 1; i < preSum.length; i++) {
preSum[i] = preSum[i - 1] + nums[i - 1];
}
}
public int sumRange(int left, int right) {
return preSum[right + 1] - preSum[left];
}
}
二维区域和检索 - 矩阵不可变
题目详情可见 :二维区域和检索 - 矩阵不可变
如果求红色区间的和,只需求preSum[4,4] - preSum[1,4] - preSum[4,1] + preSum[1,1]即可
● preSum[4,4]:黄 + 蓝 + 绿 + 红
● preSum[1,4]:黄 + 蓝
● preSum[4,1]:黄 + 绿
● preSum[1,1]:黄
代码:
class NumMatrix {
private int[][] preSum;
public NumMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
preSum = new int[m + 1][n + 1];
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] - preSum[i - 1][j - 1] + matrix[i - 1][j - 1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return preSum[row2 + 1][col2 + 1] - preSum[row1][col2 + 1] - preSum[row2 + 1][col1] + preSum[row1][col1];
}
}
和为 K 的子数组
题目详情可见 和为 K 的子数组
借鉴「两数和」的思路,利用HashMap。
代码:
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> preSum = new HashMap<>();
preSum.put(0, 1);
int sum = 0;
int res = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
int target = sum - k;
if (preSum.containsKey(target)) res += preSum.get(target);
preSum.put(sum, preSum.getOrDefault(sum, 0) + 1);
}
return res;
}
边栏推荐
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- Modify the list page on the basis of jeecg boot code generation (combined with customized components)
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- Reflex WMS中阶系列3:显示已发货可换组
- Office-DOC加载宏-上线CS
- Windows Server 2016 standard installing Oracle
- Thesis abstract translation, multilingual pure human translation
- Automated test environment configuration
猜你喜欢

基于购买行为数据对超市顾客进行市场细分(RFM模型)

My creation anniversary

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
![[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)

Every API has its foundation when a building rises from the ground

字幕翻译中翻英一分钟多少钱?

SQL Server Manager studio (SSMS) installation tutorial

Basic commands of MySQL

Machine learning plant leaf recognition

Modify the list page on the basis of jeecg boot code generation (combined with customized components)
随机推荐
How do programmers remember code and programming language?
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Pymongo gets a list of data
今日夏至 Today‘s summer solstice
My creation anniversary
Every API has its foundation when a building rises from the ground
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
SAP SD发货流程中托盘的管理
Advanced MySQL: Basics (1-4 Lectures)
Chinese English comparison: you can do this Best of luck
Apache DolphinScheduler源码分析(超详细)
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
Chapter 7 - thread pool of shared model
How to translate biomedical instructions in English
中英对照:You can do this. Best of luck祝你好运
How to translate professional papers and write English abstracts better
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Phishing & filename inversion & Office remote template
Delete external table source data