赴忽 发表于 2025-6-25 08:16:17

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

题⽬描述

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


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

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

[*]push 1 --> push 2


[*]pop 1


[*]push 3 --> push 4


[*]pop 2

import java.util.Stack;

public class Solution {

    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    /** Initialize your data structure here. */
    public MyQueue() {
      stackIn = new Stack<>(); // 负责进栈
      stackOut = new Stack<>(); // 负责出栈
    }
   
    /** Push element x to the back of queue. */
    public void push(int x) {
      stackIn.push(x);
    }
   
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {   
      dumpstackIn();
      return stackOut.pop();
    }
   
    /** Get the front element. */
    public int peek() {
      dumpstackIn();
      return stackOut.peek();
    }
   
    /** Returns whether the queue is empty. */
    public boolean empty() {
      return stackIn.isEmpty() && stackOut.isEmpty();
    }

    // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
    private void dumpstackIn(){
      if (!stackOut.isEmpty()) return;
      while (!stackIn.isEmpty()){
                stackOut.push(stackIn.pop());
      }
    }
}扩展-两个队列实现栈

队列是先进先出的规则,把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并没有变成先进后出的顺序。
所以用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质。
但是依然还是要用两个队列来模拟栈,只不过没有输入和输出的关系,而是另一个队列完全用来备份的!
class MyStack {

    Queue<Integer> queue1; // 和栈中保持一样元素的队列
    Queue<Integer> queue2; // 辅助队列

    /** Initialize your data structure here. */
    public MyStack() {
      queue1 = new LinkedList<>();
      queue2 = new LinkedList<>();
    }
   
    /** Push element x onto stack. */
    public void push(int x) {
      queue2.offer(x); // 先放在辅助队列中
      while (!queue1.isEmpty()){
            queue2.offer(queue1.poll());
      }
      Queue<Integer> queueTemp;
      queueTemp = queue1;
      queue1 = queue2;
      queue2 = queueTemp; // 最后交换queue1和queue2,将元素都放到queue1中
    }
   
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
      return queue1.poll(); // 因为queue1中的元素和栈中的保持一致,所以这个和下面两个的操作只看queue1即可
    }
   
    /** Get the top element. */
    public int top() {
      return queue1.peek();
    }
   
    /** Returns whether the stack is empty. */
    public boolean empty() {
      return queue1.isEmpty();
    }
}

[*]时间复杂度: pop为O(n),其他为O(1)
[*]空间复杂度: O(n)
优化:
其实这道题目就是用一个队列就够了。
一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
class MyStack {
    Queue<Integer> queue;
   
    public MyStack() {
      queue = new LinkedList<>();
    }
   
    public void push(int x) {
      queue.add(x);
    }
   
    public int pop() {
      rePosition();
      return queue.poll();
    }
   
    public int top() {
      rePosition();
      int result = queue.poll();
      queue.add(result);
      return result;
    }
   
    public boolean empty() {
      return queue.isEmpty();
    }

    public void rePosition(){
      int size = queue.size();
      size--;
      while(size-->0)
            queue.add(queue.poll());
    }
}

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

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 剑指offer-5、两个栈实现⼀个队列