239. 滑动窗口最大值( 队列,python)

题目:https://leetcode-cn.com/problems/sliding-window-maximum/

    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        from collections import deque
        d = deque()
        res = []
        for i in range(len(nums)):
            if len(d)<k-1:
                d.append(nums[i])
            else:
                d.append(nums[i])
                res.append(max(d))
                d.popleft()
        return res
            
发布了16 篇原创文章 · 获赞 1 · 访问量 2834

猜你喜欢

转载自blog.csdn.net/weixin_43436824/article/details/105485263
今日推荐