滑动窗口最大值的golang实现

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。

返回滑动窗口最大值

输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7]
解释: 

  滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

其实这道题就是求给定数组中获取全部K个连续元素中最大值的集合

首先我们可能会遇到三中情况

  • 当原始数组为空的,那就直接返回一个空数组
  • 如果原始数组的长度与给定的k是一样的,那么就直接去原始数组的最大值即可
  • 如果原始数组的长度大于K,那么我们就要求每个连续子数组的最大值了
对于第一种情况就非常简单了
    var result []int
    //如果切片长度为0的话,那就直接返回空切片
    if len(nums) == 0 {
        return result
    }
    

对于第二种情况:

    //如果切片长度与k一样
    if len(nums) == k {
        result = append(result, getMax(nums))
        return result
    }

对于第三种情况:

    var windowSlice []int
    index := 0
    for i := k; i <= len(nums); i++ {
        windowSlice = nums[index:i]
        result = append(result, getMax(windowSlice))
        index++
    }
    return result

这里每次都获取连续K个元素进行比较

全部代码:
package main

import "fmt"

func maxSlidingWindow(nums []int, k int) []int {
    var result []int
    //如果切片长度为0的话,那就直接返回空切片
    if len(nums) == 0 {
        return result
    }
    //如果切片长度与k一样
    if len(nums) == k {
        result = append(result, getMax(nums))
        return result
    }

    var windowSlice []int
    index := 0
    for i := k; i <= len(nums); i++ {
        windowSlice = nums[index:i]
        result = append(result, getMax(windowSlice))
        index++
    }
    return result
}

func getMax(windowSlice []int) int {
    max := windowSlice[0]
    for i := 0; i < len(windowSlice); i++ {
        if windowSlice[i] >= max {
            max = windowSlice[i]
        }
    }
    return max
}

func main() {
    nums := []int{1, 3, -1, -3, 5, 3, 6, 7}
    fmt.Println(maxSlidingWindow(nums, 3))
    // nums := []int{}
    // fmt.Println(maxSlidingWindow(nums, 0))
    // nums := []int{1, -1}
    // fmt.Println(maxSlidingWindow(nums, 1))
}

原文地址:https://www.cnblogs.com/TimLiuDream/p/10117084.html

时间: 2024-07-30 16:10:58

滑动窗口最大值的golang实现的相关文章

[leetcode]239. Sliding Window Maximum滑动窗口最大值

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

【leetcode 239. 滑动窗口最大值】解题报告

思路:滑动窗口的思想,只要是求连续子序列或者子串问题,都可用滑动窗口的思想 方法一: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> res; if (nums.size()==0) return res; int i=0; deque<int> dq; for (i=0;i<nums.size();++i) { while(!dq.empty()&

Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解

题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置|最大值 -|- [1 3 -1] -3 5

leetcode 239. 滑动窗口最大值(单调队列)

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 进阶: 你能在线性时间复杂度内解决此题吗? 示例: 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值--------------- -----[1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6

滑动窗口最大值

1.给定一个任意数组,和一个大小为w的窗口,该窗口从左到右依次滑动,得到窗口的每个状态下的最大值 1 // maxWindow.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <deque> 7 #include <vector> 8 #include <iterator> 9 10 using namespace std

[LeetCode] Sliding Window Maximum 滑动窗口最大值

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. For example,Given nums

Leetcode 239. 滑动窗口最大值

class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> ret; if(nums.size()==0) return ret; deque<int> q; for(int i=0; i<nums.size(); ++i) { if(q.empty() || nums[q.back()] > nums[i] )

【剑指offer】滑动窗口的最大值,C++实现

原创博文,转载请注明出处! # 题目 # 思路 利用C++中的双端队列保存有可能是滑动窗口最大值的下标,其中队首元素保存当前窗口最大值的下标.当滑动窗口改变时,更新队列.队列更新的规则:(1)新元素依次与队尾元素比较,如果队尾元素小于新元素,则删除队尾元素,直至队列中没有小于新元素的值.(2)更新队首元素,如果队首元素不在新滑动窗口中,则删除队首元素.(3)把每次滑动的数字的下标压入队列 找出数组中大小为3的滑动窗口的最大值,在队列中的下标一列,小括号前面的数字表示数字在数组中的下标. # 代码

《剑指offer》第五十九题I:滑动窗口的最大值

// 面试题59(一):滑动窗口的最大值 // 题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值.例如, // 如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的大小3,那么一共存在6个 // 滑动窗口,它们的最大值分别为{4, 4, 6, 6, 6, 5}, #include <cstdio> #include <vector> #include <deque> using namespace std; vector<int