题解 | #滑动窗口的最大值#
滑动窗口的最大值
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;
}
}

查看30道真题和解析