LeetCode 225 用队列实现栈

这是我参与更文挑战的第 1 天,活动详情查看: 更文挑战

题目描述

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

实现 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
复制代码

提示

  • 1 <= x <= 9
  • 最多调用100pushpoptopempty
  • 每次调用 poptop 都保证栈不为空

进阶

你能否实现每种操作的均摊时间复杂度为 O(1) 的栈?换句话说,执行 n 个操作的总时间复杂度 O(n) ,尽管其中某个操作可能需要比其他操作更长的时间。你可以使用两个以上的队列。

思路分析

用队列实现栈,涉及到队列和栈这两种数据结构,队列是先进先出,一个入口,一个出口,而栈是先进后出,只有一个入口/出口。用队列模拟栈,可以用两个队列和一个队列分别进行模拟。

首先来看两个队列(A,B)的解法,每次入栈的时候,先把 val 入到 A 队列中,因为一开始队列 A,B 都是空的,val 入队 A 后将 A 和 B 队列互换,接下来继续 push 第二个值,A 队列是空的,入队到 A 队列;这个时候 B 队列中已经有值了(因为上一次交换过),将 B 队列中的值添加到 A 队列中,这个时候 B 队列中的存放顺序就和正常栈的顺序是一样的,然后交换 A、B 队列,每次 push 都是如此;其他操作就是从 A 队列中获取。

再来看一个队列的解法,关键还是在于 push 方法,每次 push 的时候先获取队列中值的个数,然后将值放入队列中,for 循环 n 次依次将值出队然后放到队尾,这样出来的效果就类似栈的存放效果。

代码展示

解法一:push 操作是时间复杂度O(n){O(n)},其他操作复杂度是O(1){O(1)},空间复杂度是O(n){O(n)}

class MyStack {
  
    Queue<Integer> queueA = null;
    Queue<Integer> queueB = null;
    /** Initialize your data structure here. */
    public MyStack() {
        queueA = new LinkedList<>();
        queueB = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queueA.offer(x);
        while(!queueB.isEmpty()){
            queueA.offer(queueB.poll());
        }
        Queue<Integer> temp = queueA;
        queueA = queueB;
        queueB = temp;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queueB.poll();
    }
    
    /** Get the top element. */
    public int top() {
       return queueB.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queueB.isEmpty();
    }
}
复制代码

解法二:push 操作是时间复杂度O(n){O(n)},其他操作复杂度是O(1){O(1)},空间复杂度是O(n){O(n)}

class MyStack {
    Queue<Integer> queue;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        int n = queue.size();
        queue.offer(x);
        for (int i = 0; i < n; i++) {
            queue.offer(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.isEmpty();
    }
}
复制代码

总结

对应这种模拟类的题目,关键在于掌握数据结构的特性,而且这种简单的模拟栈,两个队列和一个队列都能解决,一个队列处理逻辑也很清晰。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享