239-Sliding Window Maximum

题目:返回滑动窗口中的最大值

def maxSlidingWindow(nums,k):
    if not nums:
        return []
    window,res = [],[]
    for i ,x in enumerate(nums):
        if i>=k and window[0]<=i-k:
            window.pop(0)
        while window and nums[window[-1]] <= x:
            window.pop()
        window.append(i)
        if i >= k-1:
            res.append(nums[window[0]])
    return res

 

posted @ 2019-08-16 20:41  尘世中一个迷途小书童  阅读(166)  评论(0编辑  收藏  举报