【LeetCode】225. 用队列实现栈

27.移除元素

知识点:栈;队列;

题目描述

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

实现 MyStack 类:

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

注意

  • 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例
输入: ["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

解法一:两个队列

一个队列来实现和栈一样的存储,关键就在于:队首必须是最后一个入栈的元素;这样才能实现后入先出。
另一个队列用来打辅助;

class MyStack { Queue<Integer> A, B; /** Initialize your data structure here. */ public MyStack() { A = new LinkedList(); B = new LinkedList(); } /** Push element x onto stack. */ public void push(int x) { B.add(x); while(!A.isEmpty()){ B.add(A.poll()); } Queue<Integer> temp = A; A = B; B = temp; } /** Removes the element on top of the stack and returns that element. */ public int pop() { return A.poll(); } /** Get the top element. */ public int top() { return A.peek(); } /** Returns whether the stack is empty. */ public boolean empty() { return A.isEmpty(); } } /** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
  • python
class MyStack: def __init__(self): self.queue1 = deque() self.queue2 = deque() def push(self, x: int) -> None: self.queue2.append(x) while self.queue1: self.queue2.append(self.queue1.popleft()) self.queue1, self.queue2 = self.queue2, self.queue1 def pop(self) -> int: return self.queue1.popleft() def top(self) -> int: return self.queue1[0] def empty(self) -> bool: return not len(self.queue1)

解法二:一个队列

仔细观察上面的过程,我们只要抓住核心:队列的首元素即为要入栈的元素
所以可以将入栈的元素,先进入队尾,然后把队首依次出队再接到队尾的后面;这样之前的队尾就变成了队首;

class MyStack { Queue<Integer> A; /** Initialize your data structure here. */ public MyStack() { A = new LinkedList(); } /** Push element x onto stack. */ public void push(int x) { int l = A.size(); A.add(x); for(int i = 0; i < l; i++){ A.add(A.poll()); } } /** Removes the element on top of the stack and returns that element. */ public int pop() { return A.poll(); } /** Get the top element. */ public int top() { return A.peek(); } /** Returns whether the stack is empty. */ public boolean empty() { return A.isEmpty(); } } /** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */

参考链接

用队列实现栈


__EOF__

本文作者Curryxin
本文链接https://www.cnblogs.com/Curryxin/p/15063750.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Curryxin  阅读(76)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
Live2D
欢迎阅读『【LeetCode】225. 用队列实现栈』
点击右上角即可分享
微信分享提示