File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -57,3 +57,7 @@ infinite_fib(10)
5757- 实际上借助 python 内置的 list/collections.deque 结构就很容易实现一个栈,请你尝试实现,本章我们全部使用自己编写的数据结构而没用到 python 内置的数据结构。
5858- 这里我们自己实现了 Deque,你能用 python 内置的 collections.deque 实现栈吗?有轮子能直接用的话看起来就简单多了,这里我们为了学习数据结构的实现就避免了直接使用内置结构
5959- 哪些经典算法里使用到了栈呢?
60+
61+ # Leetcode 练习
62+
63+ https://leetcode.com/problems/implement-queue-using-stacks/
Original file line number Diff line number Diff line change 22
33# NOTE: 这里拷贝的 double_link_list.py 里的代码
44
5+ from collections import deque
6+
57
68class Node (object ):
79
@@ -151,6 +153,21 @@ def pop(self):
151153 return self .deque .pop ()
152154
153155
156+ class Stack2 (object ):
157+
158+ def __init__ (self ):
159+ self ._deque = deque ()
160+
161+ def push (self , value ):
162+ return self ._deque .append (value )
163+
164+ def pop (self ):
165+ return self ._deque .pop ()
166+
167+ def empty (self ):
168+ return len (self ._deque ) == 0
169+
170+
154171def test_stack ():
155172 s = Stack ()
156173 s .push (0 )
You can’t perform that action at this time.
0 commit comments