题解 | #滑动窗口的最大值#

滑动窗口的最大值

http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788

class Solution:

def __init__(self):
    self.stack = []

def maxInWindows(self , num: List[int], size: int) -> List[int]:
    # write code here
    
    if size > len(num) or size == 0:
        return None
    
    if len(num) == 1:
        return num[0]
    
    answer = self.Mysolution(num, size)
    
    return answer

def Mysolution(self, num, size):
    # 返回所有滑动窗口里数值的最大值
    
    for i in range(len(num)):
        if i+size > len(num):
            break
        self.stack.append(num[i])
        for j in range(i+1, size+i):
            if len(self.stack) and num[j] > self.stack[len(self.stack)-1]:
                self.stack.pop()
                self.stack.append(num[j])
    
    return self.stack
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务