[LeetCode] 225. Implement Stack using Queues

2022/1/17 6:33:30

本文主要是介绍[LeetCode] 225. Implement Stack using Queues,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement the MyStack class:

  • void push(int x) Pushes element x to the top of the stack.
  • int pop() Removes the element on the top of the stack and returns it.
  • int top() Returns the element on the top of the stack.
  • boolean empty() Returns true if the stack is empty, false otherwise.
    Notes:
  • You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
  • Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
    Example1:
Input
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 2, 2, false]

Explanation
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // return 2
myStack.pop(); // return 2
myStack.empty(); // return False

Constraints:

  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, top, and empty.
  • All the calls to pop and top are valid.

这道题的思路是准备两个queue和一个integer来记录top,每次加入的时候,就加入到应用queue里,并且更新top的值。这个过程是O(1)。pop的时候,把应用queue里除了最后一个值都加入到准备queue里。同时top也要随着这个过程不停更新。最后top就停留在之前应用queue的倒数第二个值。这时候把应用queue的最后一个值弹出,这就是最后要返回的值。但是在返回之前,把应用queue和准备queue的名字互换。这个过程是O(n)。top就直接返回top,empty就返回是否size为0. 这两个过程都是O(1)
注意:

  • queue的loop的时候,必须要把size的值在第一次就取出来,queue的size会动态变化。
  • top可以用一个数值来记住。在queue的push和pop的过程中动态更新,函数被调用的时候直接返回就行。
class MyStack {
    Queue<Integer> useq;
    Queue<Integer> waitq;
    int top;

    public MyStack() {
        useq = new LinkedList<Integer>();
        waitq = new LinkedList<Integer>();
        top = Integer.MIN_VALUE;
    }
    
    public void push(int x) {
        this.useq.offer(x);
        this.top = x;
    }
    
    public int pop() {
        if (!empty()){
            int n = useq.size();
            for (int i = 0; i < n - 1; i++) {
                this.top = useq.poll();
                this.waitq.offer(top);
            }
            int rst = useq.poll();
            Queue<Integer> tmp = this.useq;
            this.useq = this.waitq;
            this.waitq = tmp;
            return rst;
        } else {
            return Integer.MIN_VALUE;
        }
    }
    
    public int top() {
        return top;
    }
    
    public boolean empty() {
        return useq.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();
 */


这篇关于[LeetCode] 225. Implement Stack using Queues的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程