leetcode-225

225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsizeis empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

解法

使用两个队列

使用类似循环队列的操作,定义两个master,aux队列,master用于存放出栈的元素,aux用于辅助队列,暂时存放前入队的元素,当主队列元素出队时,再将其导入master,然后在master中只保留一个元素,将其他元素再存到aux中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.master = list()
self.aux = list()
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
# print("这里是push")
self.master.append(x)

# print("len master",len(self.master))
if len(self.master) > 1:
self.aux = self.aux + self.master[:-1]
self.master = [self.master[-1]]
# print(self.master)
# print(self.aux)
# print("push结束")

def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
# print("--> master",self.master)
# print("--> aux",self.aux)
res = self.master.pop(0)

if len(self.aux):
self.master = self.aux
self.aux = self.master[:-1]
self.master = [self.master[-1]]

# print("==> master",self.master)
# print('==> aux',self.aux)

return res

def top(self) -> int:
"""
Get the top element.
"""
return self.master[0]


def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""

return False if len(self.master) + len(self.aux) else True



# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

使用一个队列

当某一元素入栈队时,将之前的出队,再重新入队即可,废话不多说,我们直接看动图吧,非常容易理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class MyStack:

def __init__(self):
"""
Initialize your data structure here.
"""
self.master = list()



def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
# print("这里是push")
self.master.append(x)

# 如果超过一个元素
for i in range(1,len(self.master)):
self.master.append(self.master.pop(0))

def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
res = self.master.pop(0)
return res

def top(self) -> int:
"""
Get the top element.
"""
return self.master[0]


def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return False if len(self.master) else True



# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
作者

bd160jbgm

发布于

2021-05-29

更新于

2021-05-29

许可协议