JZ64
滑动窗口的最大值
http://www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788
思路
使用Java的大顶堆(PriorityQueue)
Code
import java.util.ArrayList; import java.util.PriorityQueue; public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size) { ArrayList<Integer> ret = new ArrayList<>(); if(num == null || num.length == 0 || size <= 0|| size > num.length) return ret; //大顶堆 PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->(o2-o1)); for(int i = 0; i < size; i++) { queue.add(num[i]); } ret.add(queue.peek()); for(int i = size; i < num.length ; i++) { queue.remove(num[i-size]); queue.add(num[i]); ret.add(queue.peek()); } return ret; } }