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

滑动窗口的最大值

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

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param num int整型一维数组
     * @param size int整型
     * @return int整型ArrayList
     */
    public ArrayList<Integer> maxInWindows (int[] num, int size) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        if (size > num.length || size == 0) {
            return arrayList;
        }
        
        int start = 0;
        while ((start + size - 1) < num.length) {
            int maxVal = findMaxInWinwon(num, start, size);
            arrayList.add(maxVal);
            start++;
        }

        return arrayList;
    }

    //找出一个窗口最大值
    int findMaxInWinwon(int[] num, int l, int size) {
        int max = Integer.MIN_VALUE;
        while (size-- > 0) {
            max = num[l] >= max ? num[l] : max;
            System.out.println("l值是: " + l);
            System.out.println("num[l]" + "值是: " + num[l]);
            l++;
        }
        System.out.println("max值是: " + max);
        System.out.println("---------------");
        return max;
    }
}

全部评论

相关推荐

少年郎as:这不把公司名贴出来那我可要喷你了哦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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