【剑指offer】【java】【数组】滑动窗口的最大值

题目

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

思路

1.直接遍历所有滑动窗口,然后对滑动窗口中的值进行一遍遍历 返回即可

代码

import java.util.*;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size)
    {
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(size==0||num==null){
            return res; 
        }
        
        for(int i =0;i<=num.length-size;i++){
            int max = -1;
            for(int j=i;j<i+size;j++){
                if(num[j]>max){
                    max = num[j];
                }
            }
            res.add(max);
        }
        return res;
    }
}

总结

你可能感兴趣的:(leetcode,Java-pat,leetcode,java)