diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\351\230\237\345\210\227.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\351\230\237\345\210\227.md" index d82dbe06d43856bb4aa7def2a1d8af449be32416..140391ec548ee633f217a7c958050bccf1906df2 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\351\230\237\345\210\227.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\351\230\237\345\210\227.md" @@ -211,6 +211,60 @@ vector maxSlidingWindow(vector& nums, int k) {

======其他语言代码====== + +### python3 + +由[SCUHZS](ttps://github.com/brucecat)提供 + + +```python +from collections import deque + +class MonotonicQueue(object): + def __init__(self): + # 双端队列 + self.data = deque() + + def push(self, n): + # 实现单调队列的push方法 + while self.data and self.data[-1] < n: + self.data.pop() + self.data.append(n) + + def max(self): + # 取得单调队列中的最大值 + return self.data[0] + + def pop(self, n): + # 实现单调队列的pop方法 + if self.data and self.data[0] == n: + self.data.popleft() + + +class Solution: + def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: + # 单调队列实现的窗口 + window = MonotonicQueue() + + # 结果 + res = [] + + for i in range(0, len(nums)): + + if i < k-1: + # 先填满窗口前k-1 + window.push(nums[i]) + else: + # 窗口向前滑动 + window.push(nums[i]) + res.append(window.max()) + window.pop(nums[i-k+1]) + return res + +``` + +### java + ```java class Solution { public int[] maxSlidingWindow(int[] nums, int k) {