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 (size == 0 || num == null || num.length == 0 || num.length < size) { return res; } ArrayList<Integer> stack = new ArrayList<>(); if (size == 1) { for (int i = 0; i < num.length; ++i) { res.add(num[i]); } return res; } if (num.length == 1) { res.add(num[0]); return res; } else if (num.length == 2) { res.add(num[0] > num[1] ? num[0] : num[1]); return res; } else { int maxx = num[0]; for (int i = 0; i < size; ++i) { stack.add(num[i]); if (num[i] > maxx) { maxx = num[i]; } } // maxx = Math.max(stack.get(2), Math.max(stack.get(0), stack.get(1))); res.add(maxx); System.out.println(maxx); for (int i = size; i < num.length; ++i) { stack.add(num[i]); stack.remove(0); if (stack.contains(maxx)) { if (num[i] > maxx) { maxx = num[i]; res.add(num[i]); } else { res.add(maxx); } System.out.println(":" + maxx); } else { maxx = Math.max(stack.get(2), Math.max(stack.get(0), stack.get(1))); res.add(maxx); } } } return res; } }