publicvoidremove(Integer e){ if (e.equals(queue.getFirst())) { queue.removeFirst(); } }
public Integer getMax(){ return queue.getFirst(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ publicint[] maxSlidingWindow(int[] nums, int k) { Window window = new Window(); int[] res = newint[nums.length - k + 1]; for (int i = 0; i < k - 1; i++) { window.add(nums[i]); } for (int i = k - 1; i < nums.length; i++) { window.add(nums[i]); res[i - k + 1] = window.getMax(); window.remove(nums[i - k + 1]); } return res; } }