225、用队列实现栈

  1. 225、用队列实现栈
  2. 题解
    1. 用一个队列实现
    2. 用两个队列实现

225、用队列实现栈

使用队列实现栈的下列操作:

  • push(x) – 元素 x 入栈

  • pop() – 移除栈顶元素

  • top() – 获取栈顶元素

  • empty() – 返回栈是否为空
    注意:

  • 你只能使用队列的基本操作– 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。

  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

链接:https://leetcode-cn.com/problems/implement-stack-using-queues

题解

用一个队列实现

  • push,调用offer(x)加入新元素到队尾。然后用循环将旧元素都移动到新元素的后面,保证队列头部是栈顶元素
  • pop 直接调用poll(), 移除队列头部元素,因为入队的时候已经将栈顶元素移动到队列头部了。
  • top 等幂运算,直接调用peek(), 获取队列头部元素, 因为入队的时候已经将栈顶元素移动到队列头部了。
  • empty 直接做一个容量判断 size() == 0 即可。
class MyStack {
    private Queue<Integer> queue;
    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue.offer(x);
        // 将新增的元素移动到最前面
        for (int i = 1;i < queue.size();i++) {
            queue.add(queue.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.size() == 0;
    }
}

/**
 * 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();
 */

用两个队列实现

用两个队列实现栈,额外用一个int类型的top变量记录栈顶元素的值。

  • push,调用offer(x)加入新元素到队尾,top 记录栈顶元素。
  • pop q1直接调用poll(), 移动q1队列的前size()-1个元素到q2,用top做辅助变量,这样以来,top会始终指向栈顶元素。调用poll(),移除栈顶元素。
  • top 直接返回top即可,因为我们维护了top始终是栈顶元素的值。
  • empty 直接做一个容量判断 size() == 0 即可。
class MyStack {
    private Queue<Integer> q1;
    private Queue<Integer> q2;
    private int top;
    /** Initialize your data structure here. */
    public MyStack() {
        q1 = new LinkedList<>();// 负责加入到队列中
        q2 = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        q1.add(x);
        top = x;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        // 元素转移,留下最后一个元素用于pop
        while (q1.size() > 1) {
            top = q1.poll();
            q2.offer(top);
        }
        int res = q1.poll();// 获取最后一个元素进行返回
        Queue<Integer> t = q1;
        q1 = q2;
        q2 = t;
        return res;
    }
    
    /** Get the top element. */
    public int top() {
        return top;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q1.size() == 0;
    }
}

/**
 * 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();
 */

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:225、用队列实现栈

Count:794

Author:攀登

Created At:2020-07-26, 00:19:44

Updated At:2024-06-15, 15:52:32

Url:http://jiafeimao-gjf.github.io/2020/07/26/225%E3%80%81%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation