JZ64 滑动窗口的最大值

滑动窗口的最大值

https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&&tqId=11217&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

不找优化了

import java.util.*;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(size> num.length || size == 0) return list;


        Queue<Integer> window = new LinkedList<Integer>();

        int max = -1;
        for(int i = 0; i<num.length; i++){
            if(window.size()<size) window.offer(num[i]);
            if(window.size() == size) {
                list.add(findmax(window));
                window.poll();
            }
        }
        return list;
    }

    public int findmax(Queue<Integer> window){
        int max = -1;
        for(int i: window){
            max = Math.max(max,i);
        }
        return max;
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务