题解 | #滑动窗口的最大值#
滑动窗口的最大值
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 > len(num) || size == 0 { return []int{} } var queue1 []int var ans []int var maxValue int for k := 0; k < size; k++ { queue1 = append(queue1, num[k]) maxValue = max(maxValue, num[k]) } ans = append(ans, maxValue) for i := size; i < len(num); i++ { queue1 = queue1[1:] queue1 = append(queue1, num[i]) value := 0 for j := 0; j < size; j++ { value = max(value, queue1[j]) } ans = append(ans, value) } return ans } func max(a, b int) int { if a >= b { return a } return b }