当前位置:网站首页>【栈和队列专题】—— 滑动窗口
【栈和队列专题】—— 滑动窗口
2022-04-23 20:21:00 【[email protected]】
双端队列
1.双端队列的概念:
普通队列是可以队首删除元素,队尾添加元素。对于双端队列,在队首、队尾都可以进行入队和出队的操作。
- 在一端进行出队和入队的操作,就有点类似栈的意思了,后入先出
- 也存在受限的双端队列:
一端可以入队、出队操作,另一端只能进行入队操作;
一端可以入队、出队操作,另一端只能进行出队操作。
2.双端队列的基本操作:
queue.peek() == queue.peekFirst() 查看队首元素
queue.peekLast() 查看队尾元素
queue.add() == queue.addFirst() 从队首添加元素
queue.addLast() 从队尾添加元素
queue.poll() == queue.pollFirst() 删除队首元素
queue.pollLast() 删除队尾元素
LeetCode 239: 滑动窗口最大值
️ 解题思路:
(1)创建一个结果集数组,大小为nums.length - k + 1
(2)创建一个双端队列,保存放到队列中的元素的索引。
如果队列为空,那么直接将元素索引进入队列;如果队列不为空,而且新进入的元素比上一个元素大,那么弹出上一个元素,再将当前元素入队
因为当前窗口中的最大值肯定不能是比当前待进入元素小的值
(3)判断当前最大值是否还在滑动窗口中,如果已经不再窗口中了,要将这个元素的索引从队首弹出
(4)将窗口中的最大值保存到结果集数组中
️ 代码部分:
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
//特殊情况处理
if(nums == null || nums.length < 2){
return nums;
}
//结果集数组
int [] res = new int [nums.length - k + 1];
//用双端队列来保存数组索引
LinkedList<Integer> queue = new LinkedList();
//遍历数组
for(int i = 0; i < nums.length; i++){
//如果队列不为空,当前对内元素小于下一个入队的元素,那么弹出队内的元素
while(!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]){
queue.pollLast();
}
//将当前元素的索引入队
queue.addLast(i);
//判断队列中的值是否有效
if(queue.peek() <= i - k){
queue.poll();
}
//将滑块中的最大值保存到结果集中,主要控制好第一个录入的元素即可
if(i + 1 >= k){
res[i - k + 1] = nums[queue.peek()];
}
}
return res;
}
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61323055/article/details/124359709
边栏推荐
- R语言ggplot2可视化分面图(facet_wrap)、使用lineheight参数自定义设置分面图标签栏(灰色标签栏)的高度
- Paper writing 19: the difference between conference papers and journal papers
- R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
- R language uses the preprocess function of caret package for data preprocessing: BoxCox transform all data columns (convert non normal distribution data columns to normal distribution data and can not
- R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
- PostgreSQL basic functions
- Commit and ROLLBACK in DCL of 16mysql
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- 考研英语唐叔的语法课笔记
- Local call feign interface message 404
猜你喜欢
Redis cache penetration, cache breakdown, cache avalanche
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Linux64Bit下安装MySQL5.6-不能修改root密码
Servlet learning notes
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
LeetCode动态规划训练营(1~5天)
What is the difference between a host and a server?
Numpy Index & slice & iteration
考研英语唐叔的语法课笔记
随机推荐
nc基础用法4
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
Five minutes to show you what JWT is
16MySQL之DCL 中 COMMIT和ROllBACK
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
PCL点云处理之直线与平面的交点计算(五十三)
Operation of numpy array
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
【PTA】整除光棍
How to protect ECs from hacker attacks?
Es index (document name) fuzzy query method (database name fuzzy query method)
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Remote code execution in Win 11 using wpad / PAC and JScript 3
Redis installation (centos7 command line installation)
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk