https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
给定一个数组 nums
和滑动窗口的大小 k
,请找出所有滑动窗口里的最大值。
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0){
return nums;
}
int[] data=new int[nums.length-k+1];
Deque<Integer> deque=new LinkedList<>();
for(int j=0;j<k&&j<nums.length;j++){
while(!deque.isEmpty()&&deque.peekLast()<nums[j]){
deque.removeLast();
}
deque.addLast(nums[j]);
}
data[0]=deque.peekFirst();
for(int i=k;i<nums.length;i++){
if(deque.peekFirst()==nums[i-k]){
deque.removeFirst();
}
while(!deque.isEmpty()&&deque.peekLast()<nums[i]){
deque.removeLast();
}
deque.addLast(nums[i]);
data[i-k+1]=deque.peekFirst();
}
return data;
}
}