leetcode 225. Implement Stack using Queues implement stack with queue (simple)

Time:2022-11-25

1. The main idea of ​​the topic

Please use only two queues to implement a last-in-first-out (LIFO) stack, and support all four operations (push, top, pop, and empty) of a normal stack.

Implement the MyStack class:

void push(int x) pushes the element x onto the top of the stack.
int pop() removes and returns the top element of the stack.
int top() Returns the top element of the stack.
boolean empty() Returns true if the stack is empty; otherwise, returns false.

Notice:

You can only use the basic operations of the queue – that is, push to back, peek/pop from front, size and is empty.
Your language may not support queues. You can use list (list) or deque (double-ended queue) to simulate a queue, as long as it is a standard queue operation.

Example:

enter:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
output:
[null, null, null, 2, 2, false]

explain:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack. top(); // returns 2
myStack. pop(); // returns 2
myStack.empty(); // returns False

hint:

  • 1 <= x <= 9
  • Up to 100 calls to push, pop, top and empty
  • Each call to pop and top ensures that the stack is not empty

Advanced:Can you implement a stack with just a queue.

2. Problem-solving ideas

Use the queue to insert the newly added number to the front each time, so that the order in which the queue is saved is opposite to the order of the stack, and the way they are taken out is also reversed, so the opposite is true, which is the order we need. I can directly operate on the queue q. After adding a new element x at the end of the queue, arrange all the elements in front of x to be taken out in order and add them to the end of the queue, so that x can be taken out directly next time, which is in line with the stack The last-in-first-out feature, the other three operations are operations that directly call the queue.

3. Problem-solving method

3.1 Java implementation

public class MyStack {

    Queue<Integer> q;

    public MyStack() {
        // int size(): get the queue length;
        //boolean add(E)/boolean offer(E): add elements to the end of the queue;
        //E remove()/E poll(): Get the first element of the queue and delete it from the queue;
        //E element()/E peek(): Get the first element of the queue but not delete it from the queue.
        q = new LinkedList<>();
    }

    public void push(int x) {
        q.add(x);
        for (int i = 0; i < q.size() - 1; i++) {
            q.add(q.poll());
        }
    }

    public int pop() {
        return q.poll();
    }

    public int top() {
        return q.peek();
    }

    public boolean empty() {
        return q.isEmpty();
    }
}

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

Four, summary notes

  • 2022/8/19 The weather is like the face of a big girl, it changes as it says