当前位置:网站首页>【剑指 Offer】59 - I. 滑动窗口的最大值
【剑指 Offer】59 - I. 滑动窗口的最大值
2022-07-07 16:36:00 【LuZhouShiLi】
剑指 Offer 59 - I. 滑动窗口的最大值
题目
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
思路
- 遍历给定数组中的元素,如果队列不为空且当前考察的元素大于等于队尾元素,则将队尾元素移除,直到队列为空或者当前考察元素小于新的队尾元素
- 当队首元素的下标小于滑动窗口的左侧边界Left表示队首元素已经不再属于滑动窗口,将其从队首元素移除
- 由于数组下标从0开始,因此当窗口的右边界right + 1>= k,意味着窗口已经形成。此时队首元素就是该窗口内的最大值。
代码
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0)
{
return new int[0];
}
// 窗口个数
int[] res = new int[nums.length - k + 1];
LinkedList<Integer> queue = new LinkedList<>();
// 遍历数组中元素, right表示滑动窗口右边界
for(int right = 0; right < nums.length; right++)
{
// 如果队列不为空 并且当前的元素大于或者等于队尾元素 将队尾元素移除
// 直到 队列为空 或者当前的元素小于新的队尾元素
while(!queue.isEmpty() && nums[right] >= nums[queue.peekLast()]){
queue.removeLast();
}
// 存储元素下标
queue.addLast(right);
// 计算窗口左侧边界
int left = right - k + 1;
//当队首元素的下标小于滑动窗口左侧边界Left
// 表示队首元素已经不再滑动窗口内部 将其从队首中移除
if(queue.peekFirst() < left)
{
queue.removeFirst();
}
// 下边从0开始 所以当right + 1 >= k 说明滑动窗口已经形成 队首元素就是窗口的最大值
if(right + 1 >= k)
{
res[left] = nums[queue.peekFirst()];// 将队首元素存储到res中
}
}
return res;
}
}
边栏推荐
- [answer] if the app is in the foreground, the activity will not be recycled?
- Improve application security through nonce field of play integrity API
- 手撕Nacos源码(先撕客户端源码)
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- 2022年理财产品的一般收益率是多少?
- Hash, bitmap and bloom filter for mass data De duplication
- 现在网上期货开户安全吗?国内有多少家正规的期货公司?
- Summary of debian10 system problems
- SQLite SQL exception near "with": syntax error
- 2021年全国平均工资出炉,你达标了吗?
猜你喜欢

手撕Nacos源码(先撕客户端源码)

Nunjuks template engine

ICer知识点杂烩(后附大量题目,持续更新中)

C语言中匿名的最高境界

Mobile app takeout ordering personal center page
![[4500 word summary] a complete set of skills that a software testing engineer needs to master](/img/82/acae52928b3ab48e9ecbf4ec436e5e.jpg)
[4500 word summary] a complete set of skills that a software testing engineer needs to master

Improve application security through nonce field of play integrity API

2021年全国平均工资出炉,你达标了吗?
![[tpm2.0 principle and Application guide] Chapter 5, 7 and 8](/img/38/93fd986916193803bbd90805f832fa.png)
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8

Sanxian Guidong JS game source code
随机推荐
同消费互联网的较为短暂的产业链不同,产业互联网的产业链是相当漫长的
Tips of this week 135: test the contract instead of implementation
Test for 3 months, successful entry "byte", my interview experience summary
强化学习-学习笔记8 | Q-learning
Kirk Borne的本周学习资源精选【点击标题直接下载】
回归测试的分类
Chapter 3 business function development (to remember account and password)
zdog. JS rocket turn animation JS special effects
4种常见的缓存模式,你都知道吗?
Summary of debian10 system problems
Disk storage chain B-tree and b+ tree
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
数学分析_笔记_第11章:Fourier级数
[principles and technologies of network attack and Defense] Chapter 5: denial of service attack
回归问题的评价指标和重要知识点总结
Sanxian Guidong JS game source code
CVPR 2022丨学习用于小样本语义分割的非目标知识
go语言的字符串类型、常量类型和容器类型
Tips of this week 141: pay attention to implicit conversion to bool
Chapter 3 business function development (user access project)