当前位置:网站首页>前缀和数组系列
前缀和数组系列
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;
}
边栏推荐
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- How to translate biomedical instructions in English
- [ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
- [ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
- Apache dolphin scheduler source code analysis (super detailed)
- LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
- 机器学习植物叶片识别
- Leetcode daily question (1870. minimum speed to arrive on time)
- The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
- Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
猜你喜欢
ROS2安装及基础知识介绍
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
E-book CHM online CS
Reflex WMS中阶系列3:显示已发货可换组
How to do a good job in financial literature translation?
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Machine learning plant leaf recognition
Every API has its foundation when a building rises from the ground
AI on the cloud makes earth science research easier
云上有AI,让地球科学研究更省力
随机推荐
Office-DOC加载宏-上线CS
Wish Dragon Boat Festival is happy
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
How to translate professional papers and write English abstracts better
Biomedical localization translation services
论文翻译英译中,怎样做翻译效果好?
MySQL5.72. MSI installation failed
SQL Server Manager studio (SSMS) installation tutorial
Day 248/300 thoughts on how graduates find jobs
自动化测试环境配置
利用快捷方式-LNK-上线CS
Distributed system basic (V) protocol (I)
How do programmers remember code and programming language?
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Leetcode - 152 product maximum subarray
英语论文翻译成中文字数变化
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
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype