找回密码
 立即注册
首页 业界区 安全 剑指offer-5、两个栈实现⼀个队列

剑指offer-5、两个栈实现⼀个队列

赴忽 2025-6-25 08:16:17
题⽬描述

⽤两个栈来实现⼀个队列,完成队列的 Push 和 Pop 操作。 队列中的元素为 int 类型。
思路及解答


  • 栈的特性是先进后出
  • 队列的特性是先进先出
有两个栈 stack1 , stack2 ;

  • 如果有新的数据进⼊,那么我们可以直接 push 到 stack1 ;
  • 如果需要取出数据,那么我们优先取出 stack2 的数据,如果 stack2 ⾥⾯数据是空的,那么我们需要把所有的 stack1 的数据倒⼊ stack2 。再从 stack2 取数据。
例如:

  • push 1 --> push 2
1.png


  • pop 1
2.png


  • push 3 --> push 4
3.png


  • pop 2
4.png
  1. import java.util.Stack;
  2. public class Solution {
  3.     Stack<Integer> stackIn;
  4.     Stack<Integer> stackOut;
  5.     /** Initialize your data structure here. */
  6.     public MyQueue() {
  7.         stackIn = new Stack<>(); // 负责进栈
  8.         stackOut = new Stack<>(); // 负责出栈
  9.     }
  10.    
  11.     /** Push element x to the back of queue. */
  12.     public void push(int x) {
  13.         stackIn.push(x);
  14.     }
  15.    
  16.     /** Removes the element from in front of queue and returns that element. */
  17.     public int pop() {   
  18.         dumpstackIn();
  19.         return stackOut.pop();
  20.     }
  21.    
  22.     /** Get the front element. */
  23.     public int peek() {
  24.         dumpstackIn();
  25.         return stackOut.peek();
  26.     }
  27.    
  28.     /** Returns whether the queue is empty. */
  29.     public boolean empty() {
  30.         return stackIn.isEmpty() && stackOut.isEmpty();
  31.     }
  32.     // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
  33.     private void dumpstackIn(){
  34.         if (!stackOut.isEmpty()) return;
  35.         while (!stackIn.isEmpty()){
  36.                 stackOut.push(stackIn.pop());
  37.         }
  38.     }
  39. }
复制代码
扩展-两个队列实现栈

队列是先进先出的规则,把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并没有变成先进后出的顺序。
所以用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质。
但是依然还是要用两个队列来模拟栈,只不过没有输入和输出的关系,而是另一个队列完全用来备份的!
  1. class MyStack {
  2.     Queue<Integer> queue1; // 和栈中保持一样元素的队列
  3.     Queue<Integer> queue2; // 辅助队列
  4.     /** Initialize your data structure here. */
  5.     public MyStack() {
  6.         queue1 = new LinkedList<>();
  7.         queue2 = new LinkedList<>();
  8.     }
  9.    
  10.     /** Push element x onto stack. */
  11.     public void push(int x) {
  12.         queue2.offer(x); // 先放在辅助队列中
  13.         while (!queue1.isEmpty()){
  14.             queue2.offer(queue1.poll());
  15.         }
  16.         Queue<Integer> queueTemp;
  17.         queueTemp = queue1;
  18.         queue1 = queue2;
  19.         queue2 = queueTemp; // 最后交换queue1和queue2,将元素都放到queue1中
  20.     }
  21.    
  22.     /** Removes the element on top of the stack and returns that element. */
  23.     public int pop() {
  24.         return queue1.poll(); // 因为queue1中的元素和栈中的保持一致,所以这个和下面两个的操作只看queue1即可
  25.     }
  26.    
  27.     /** Get the top element. */
  28.     public int top() {
  29.         return queue1.peek();
  30.     }
  31.    
  32.     /** Returns whether the stack is empty. */
  33.     public boolean empty() {
  34.         return queue1.isEmpty();
  35.     }
  36. }
复制代码

  • 时间复杂度: pop为O(n),其他为O(1)
  • 空间复杂度: O(n)
优化:
其实这道题目就是用一个队列就够了。
一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
  1. class MyStack {
  2.     Queue<Integer> queue;
  3.    
  4.     public MyStack() {
  5.         queue = new LinkedList<>();
  6.     }
  7.    
  8.     public void push(int x) {
  9.         queue.add(x);
  10.     }
  11.    
  12.     public int pop() {
  13.         rePosition();
  14.         return queue.poll();
  15.     }
  16.    
  17.     public int top() {
  18.         rePosition();
  19.         int result = queue.poll();
  20.         queue.add(result);
  21.         return result;
  22.     }
  23.    
  24.     public boolean empty() {
  25.         return queue.isEmpty();
  26.     }
  27.     public void rePosition(){
  28.         int size = queue.size();
  29.         size--;
  30.         while(size-->0)
  31.             queue.add(queue.poll());
  32.     }
  33. }
复制代码

  • 时间复杂度: pop为O(n),其他为O(1)
  • 空间复杂度: O(n)

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册