滑动窗口
滑动窗口的最大值
http://www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788
win中储存滑动窗口中非递增的序列(或者序列对应的下标都可)。
import java.util.*; public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size){ ArrayList<Integer> list=new ArrayList<>(); if(num==null||num.length==0||size<=0) return list; Deque<Integer> win=new ArrayDeque<>(); for(int l=0, r=0; r<num.length; r++){ if(r-l>size-1) { if(win.peekFirst()==num[l]) win.removeFirst(); l++; } while(!win.isEmpty()&&win.peekLast()<num[r]) win.removeLast(); win.addLast(num[r]); if(r-l==size-1) list.add(win.peekFirst()); } return list; } }