用栈实现队列
使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
思路
当s2为空时,可以把s1的所有元素取出再添加进s2,这时候s2中元素就是先进先出顺序了。
注意栈的元素访问,只能是pop()或者-1
例:
原栈中元素为 1 2 3
将其挨个弹出,放到第二个栈中,变为3 2 1
然后1就是新的队列的第一个元素,就可以执行弹出pop操作
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack1 = []
self.stack2 = []
def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.stack1.append(x) # 只在第一个栈中存放新push进来的元素
def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
if not self.stack2:
# 如果第二个栈为空,就把第一个栈中的元素全部放到第二个栈中
# 这样,第二个栈就满足先进先出的功能
#(先放进第一个栈的元素现在在第二个栈的栈顶,符合先进先出)
while self.stack1:
self.stack2.append(self.stack1.pop())
# 如果第二个栈中有元素,就直接pop,因为第二个栈已经满足队列功能
return self.stack2.pop()
def peek(self) -> int:
"""
Get the front element.
"""
# 和pop操作类似,只是不再弹出,直接取值
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2[-1]
def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return not self.stack1 and not self.stack2
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
用队列实现栈
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
思路
使用一个队列即可,但是要用一个变量记录栈顶元素,也即每次新push进来的元素就是新的栈顶元素top_elem,进行pop操作,需要将队列的元素进行倒置,也即依次把队头元素取出,放到队尾(栈顶元素对应原来的队列尾部,倒置之后,栈顶元素位于队列第一个位置,方便弹出),最后剩余两个元素的时候,要停下来保存栈顶元素,因为,最后删除栈顶元素后,栈顶元素需要更新,新的栈顶元素是原队列中的倒数第二个元素,原队列的倒数第一个元素(栈顶元素)被删除.
例:
原队列元素为 1 2 3 4
倒置之后变为 3 4 1 2
记录3为新的栈顶元素,然后继续倒置一次,将3放到最后,变为 4 1 2 3
删除4,完成pop操作
注意,队列只能使用pop(0)和[0]操作
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = []
self.top_elem = 0
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.queue.append(x)
self.top_elem = x
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
size = len(self.queue)
while size > 2:
self.queue.append(self.queue.pop(0))
size -= 1
self.top_elem = self.queue[0]
self.queue.append(self.queue.pop(0))
return self.queue.pop(0)
def top(self) -> int:
"""
Get the top element.
"""
return self.top_elem
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return not self.queue
# 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()
用队列实现栈没啥亮点,但是用双栈实现队列是值得学习的。
出栈顺序本来就和入栈顺序相反,但是从栈s1搬运元素到s2之后,s2中元素出栈的顺序就变成了队列的先进先出顺序,这个特性有点类似「负负得正」。