题解 | #滑动窗口的最大值#

滑动窗口的最大值

http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788

技巧:存放的下标

import java.util.*;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        Deque<Integer> que = new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        for(int i = 0; i < num.length; i++){
            while(!que.isEmpty()&&i-que.getFirst()>=size)que.removeFirst();
            while(!que.isEmpty()&&num[que.getLast()]< num[i])que.removeLast();
            que.add(i);
            if(size>0&&i>=size-1)list.add(num[que.getFirst()]);
        }
        return list;
    }
}
全部评论

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务