https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
示例:
输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7]
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0 || nums == null) return new int[0];
Deque<Integer> deque = new LinkedList<>();
int[] res = new int[nums.length - k + 1];
int t = 0;
for(int i = 0; i < nums.length; i++){
if(!deque.isEmpty() && i - deque.peekFirst() >= k){
deque.pollFirst();
}
while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]){
deque.pollLast();
}
deque.offer(i);
// if(i>=2) //
if(i >= k-1) res[t++] = nums[deque.peek()];
}
return res;
}
}
//也可以加对应元素,判断对应位置的元素和deque对应是否相等