leetcode:#59 - I. 滑动窗口的最大值
采用Java实现
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return new int[0];
}
int numsLength = nums.length;
int[] maxResult = new int[numsLength - k + 1];
ArrayDeque<Integer> help = new ArrayDeque<>();
for (int i = 0, j = 0; i < numsLength; i++) {
if (!help.isEmpty() && help.peekFirst() < i + 1 - k) {
help.pollFirst();
}
while (!help.isEmpty() && nums[i] > nums[help.peekLast()]) {
help.pollLast();
}
help.addLast(i);
if (i + 1 >= k) {
maxResult[j++] = nums[help.peekFirst()];
}
}
return maxResult;
}
}