题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @param size int整型 * @return int整型ArrayList */ public ArrayList<Integer> maxInWindows (int[] num, int size) { // write code here ArrayList<Integer> res = new ArrayList<>(); if(num.length<size||size==0){ return res; } Deque<Integer> window = new LinkedList<>(); int max = 0; for(int i = 0; i < size; i++){ window.offer(num[i]); max = Math.max(max,num[i]); } res.add(max); int a; int b; for(int i = size; i < num.length; i++){ a = num[i]; window.offer(a); b = window.poll(); System.out.println(a + " " + b); if(a>=max){ max = a; }else if(b < max){ }else{ max=0; for(int j = i-size+1; j <= i; j++){ max = Math.max(max,num[j]); System.out.print(num[j]+" "); } System.out.println(); } res.add(max); } return res; } }