题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
package main /** * * @param num int整型一维数组 * @param size int整型 * @return int整型一维数组 */ func maxInWindows( num []int , size int ) []int { // write code here if size == 0 || size > len(num) { return nil } var queue []int var res []int for i := 0; i < len(num); i++ { for len(queue) > 0 && num[queue[len(queue)-1]] < num[i] { queue = queue[:len(queue)-1] } queue = append(queue, i) if i >= size && queue[0] == i-size { queue = queue[1:] } if i >= size-1 { res = append(res, num[queue[0]]) } } return res }
go golang