File tree Expand file tree Collapse file tree 1 file changed +18
-25
lines changed Expand file tree Collapse file tree 1 file changed +18
-25
lines changed Original file line number Diff line number Diff line change @@ -331,43 +331,34 @@ class Solution:
331
331
332
332
> 使用栈实现队列
333
333
334
+ 两个栈:输入栈和输出栈。
335
+
334
336
``` Python
335
337
class MyQueue :
336
338
337
339
def __init__ (self ):
338
- self .cache = []
339
- self .out = []
340
+ self .instack = []
341
+ self .outstack = []
340
342
341
343
def push (self , x : int ) -> None :
342
- """
343
- Push element x to the back of queue.
344
- """
345
- self .cache.append(x)
344
+ self .instack.append(x)
346
345
347
346
def pop (self ) -> int :
348
- """
349
- Removes the element from in front of queue and returns that element.
350
- """
351
- if len (self .out) == 0 :
352
- while len (self .cache) > 0 :
353
- self .out.append(self .cache.pop())
354
-
355
- return self .out.pop()
347
+ if not self .outstack:
348
+ while self .instack:
349
+ self .outstack.append(self .instack.pop())
350
+
351
+ return self .outstack.pop()
356
352
357
353
def peek (self ) -> int :
358
- """
359
- Get the front element.
360
- """
361
- if len (self .out) > 0 :
362
- return self .out[- 1 ]
363
- else :
364
- return self .cache[0 ]
354
+ if not self .outstack:
355
+ while self .instack:
356
+ self .outstack.append(self .instack.pop())
357
+
358
+ return self .outstack[- 1 ]
365
359
366
360
def empty (self ) -> bool :
367
- """
368
- Returns whether the queue is empty.
369
- """
370
- return len (self .cache) == 0 and len (self .out) == 0
361
+ return not (self .instack or self .outstack)
371
362
```
372
363
373
364
### [ binary-tree-level-order-traversal] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ )
@@ -519,6 +510,8 @@ class Solution:
519
510
520
511
> 求滑动窗口中的最大元素
521
512
513
+ 先掌握堆的做法即可。
514
+
522
515
``` Python
523
516
class Solution :
524
517
def maxSlidingWindow (self , nums : List[int ], k : int ) -> List[int ]:
You can’t perform that action at this time.
0 commit comments