leetcode 239-Sliding Window Maximum(hard)
2021-07-08 10:06
标签:ast 就会 nbsp 匿名函数 dex tor turn imu only Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. 1. deque use a deque to store the promising candidates for the max value in the sliding window. If the number is out of the bound of the window, then it can not be a candidate. If num[i] is inside the window, but num[i] But, what should we store in deque, at first, I think of store the value of numbers, but how can we know whether the number is out of the sliding window? So we need to store the index of the number, and delete it when we find the index in the queue is out of the window. So, how do we get the value? It is saved in the number array. 注意,当存的是index的时候尤其要小心,很容易就会把index和value弄混,比如自己写的时候就把poll出的index直接当value用了。 2. priorityqueue use a priorityqueue to store the numbers inside the window, notice that for basic priorityqueue, the value is increasing, but here, we need it to be decreasing, so we need to add a comparator. comparator写的太少,需要多练习多巩固,记清楚该怎么写,匿名函数该怎么写。 leetcode 239-Sliding Window Maximum(hard) 标签:ast 就会 nbsp 匿名函数 dex tor turn imu only 原文地址:https://www.cnblogs.com/yshi12/p/9739780.htmlclass Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0) return new int[0];
Deque
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0) return new int[0];
int[] ans=new int[nums.length-k+1];
PriorityQueue
文章标题:leetcode 239-Sliding Window Maximum(hard)
文章链接:http://soscw.com/index.php/essay/102339.html