Given an array and the size of sliding windows, find the maximum value in each sliding window

By comparing the elements of each window

import java.util.*;
public class Solution {
    
    
    public ArrayList<Integer> maxInWindows(int [] num, int size){
    
    
        ArrayList<Integer> result = new ArrayList<Integer>();
        //边界检查
        if(num.length<=0 || size <= 0){
    
    
            return result;
        }
        int i = 0;
        int temp = 0;
        for(i=0;i<=num.length-size;i++){
    
    
            //将当前值设为最大值
            temp = num[i];
            //循环滑动窗口
            for(int j=i;j<i+size-1;j++){
    
    
                if(temp<num[j+1]){
    
    
                    //交换
                    temp = num[j+1];
                }
            }
            //添加到list中
            result.add(temp);
        }
        return result;
    }
}

Guess you like

Origin blog.csdn.net/weixin_42643321/article/details/107602148