Posted on 2023-08-28 20:42
Uriel 阅读(29)
评论(0) 编辑 收藏 引用 所属分类:
模拟 、
数据结构 、
闲来无事重切Leet Code
用队列模拟栈操作
1 #225
2 #Runtime: 7 ms (Beats 97.10%)
3 #Memory: 13.3 MB (Beats 55.58%)
4
5 class MyStack(object):
6
7 def __init__(self):
8 self.que = deque()
9
10 def push(self, x):
11 """
12 :type x: int
13 :rtype: None
14 """
15 self.que.append(x)
16
17 def pop(self):
18 """
19 :rtype: int
20 """
21 for _ in range(len(self.que) - 1):
22 self.que.append(self.que.popleft())
23 return self.que.popleft()
24
25
26 def top(self):
27 """
28 :rtype: int
29 """
30 return self.que[-1]
31
32
33 def empty(self):
34 """
35 :rtype: bool
36 """
37 return len(self.que) == 0
38
39
40
41 # Your MyStack object will be instantiated and called as such:
42 # obj = MyStack()
43 # obj.push(x)
44 # param_2 = obj.pop()
45 # param_3 = obj.top()
46 # param_4 = obj.empty()