1212
1313## Stack 栈
1414
15- [ min-stack] ( https://leetcode-cn.com/problems/min-stack/ )
15+ ### [ min-stack] ( https://leetcode-cn.com/problems/min-stack/ )
1616
1717> 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
1818
@@ -40,7 +40,7 @@ class MinStack:
4040 return self .stack[- 1 ][1 ]
4141```
4242
43- [ evaluate-reverse-polish-notation] ( https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ )
43+ ### [ evaluate-reverse-polish-notation] ( https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ )
4444
4545> ** 波兰表达式计算** > ** 输入:** [ "2", "1", "+", "3", "* "] > ** 输出:** 9
4646> ** 解释:** ((2 + 1) \* 3) = 9
@@ -78,7 +78,7 @@ class Solution:
7878 return stack[0 ]
7979```
8080
81- [ decode-string] ( https://leetcode-cn.com/problems/decode-string/ )
81+ ### [ decode-string] ( https://leetcode-cn.com/problems/decode-string/ )
8282
8383> 给定一个经过编码的字符串,返回它解码后的字符串。
8484> s = "3[ a] 2[ bc] ", 返回 "aaabcbc".
@@ -129,7 +129,7 @@ def DFS(vertex):
129129 return
130130```
131131
132- [ binary-tree-inorder-traversal] ( https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ )
132+ ### [ binary-tree-inorder-traversal] ( https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ )
133133
134134> 给定一个二叉树,返回它的* 中序* 遍历。
135135
@@ -154,7 +154,7 @@ class Solution:
154154 return inorder
155155```
156156
157- [ clone-graph] ( https://leetcode-cn.com/problems/clone-graph/ )
157+ ### [ clone-graph] ( https://leetcode-cn.com/problems/clone-graph/ )
158158
159159> 给你无向连通图中一个节点的引用,请你返回该图的深拷贝(克隆)。
160160
@@ -212,9 +212,7 @@ class Solution:
212212 return visited[start]
213213```
214214
215-
216-
217- [ number-of-islands] ( https://leetcode-cn.com/problems/number-of-islands/ )
215+ ### [ number-of-islands] ( https://leetcode-cn.com/problems/number-of-islands/ )
218216
219217> 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
220218
@@ -258,7 +256,7 @@ class Solution:
258256 return num_island
259257```
260258
261- [ largest-rectangle-in-histogram] ( https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ )
259+ ### [ largest-rectangle-in-histogram] ( https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ )
262260
263261> 给定 _ n_ 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
264262> 求在该柱状图中,能够勾勒出来的矩形的最大面积。
@@ -347,7 +345,7 @@ class Solution:
347345
348346常用于 BFS 宽度优先搜索
349347
350- [ implement-queue-using-stacks] ( https://leetcode-cn.com/problems/implement-queue-using-stacks/ )
348+ ### [ implement-queue-using-stacks] ( https://leetcode-cn.com/problems/implement-queue-using-stacks/ )
351349
352350> 使用栈实现队列
353351
@@ -390,7 +388,7 @@ class MyQueue:
390388 return len (self .cache) == 0 and len (self .out) == 0
391389```
392390
393- [ binary-tree-level-order-traversal] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ )
391+ ### [ binary-tree-level-order-traversal] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ )
394392
395393> 二叉树的层序遍历
396394
@@ -420,7 +418,7 @@ class Solution:
420418 return levels
421419```
422420
423- [ 01-matrix] ( https://leetcode-cn.com/problems/01-matrix/ )
421+ ### [ 01-matrix] ( https://leetcode-cn.com/problems/01-matrix/ )
424422
425423> 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
426424> 两个相邻元素间的距离为 1
@@ -491,7 +489,44 @@ class Solution:
491489 return dist
492490```
493491
492+ ## 补充:单调队列
493+
494+ ### [ sliding-window-maximum] ( https://leetcode-cn.com/problems/sliding-window-maximum/ )
495+
496+ > 求滑动窗口中的最大元素
494497
498+ ``` Python
499+ class Solution :
500+ def maxSlidingWindow (self , nums : List[int ], k : int ) -> List[int ]:
501+
502+ N = len (nums)
503+ if N * k == 0 :
504+ return []
505+
506+ if k == 1 :
507+ return nums[:]
508+
509+ # define a max queue
510+ maxQ = collections.deque()
511+
512+ def push (i ):
513+ if maxQ and maxQ[0 ] == i - k:
514+ maxQ.popleft()
515+
516+ while maxQ and nums[maxQ[- 1 ]] < nums[i]:
517+ maxQ.pop()
518+
519+ maxQ.append(i)
520+ return
521+
522+ result = []
523+ for i in range (N):
524+ push(i)
525+ if i >= k - 1 :
526+ result.append(nums[maxQ[0 ]])
527+
528+ return result
529+ ```
495530
496531## 总结
497532
0 commit comments