Title Description:
Please use only two queues to implement a last in first out (LIFO) stack, and support all four operations of ordinary queues (push, top, pop and empty).
Implement MyStack class:
void push(int x) pushes element X to the top of the stack.
int pop() removes and returns the top of stack element.
int top() returns the top element of the stack.
boolean empty() returns true if the stack is empty; Otherwise, false is returned.
be careful:
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 or deque to simulate a queue, as long as it is a standard queue operation.
Example:
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
Tips:
1 <= x <= 9
Call push, pop, top and empty up to 100 times
Ensure that the stack is not empty every time pop and top are called
Advanced: can you achieve a stack with average time complexity of O(1) for each operation? In other words, the total time complexity of performing n operations is O(n), although one of them may take longer than the others. You can use more than two queues.
Source: LeetCode
Link: https://leetcode-cn.com/problems/implement-stack-using-queues
Idea 1: stack is characterized by last in first out. The queue we use is first in first out. We can use two queues to realize it. It is ok if the last element entering the queue is the head element of the queue.
In the stack operation, first queue the elements to q2, and then queue all the elements of q1 out of the queue and into q2 in turn
At this time, the header element of q2 is the newly stacked element, and then q1
And q2 are interchanged, then the element of q1 is the element in the stack, and the front end and back end of q1 correspond to the top and bottom of the stack respectively.
So take the top element of the stack, pop up the element, judge the empty operation, and directly operate or judge q1
Answer 1:
Queue<Integer> q1; Queue<Integer> q2; /** Initialize your data structure here. */ public MyStack() { q1=new LinkedList<>(); q2=new LinkedList<>(); } /** Push element x onto stack. */ //The first method //q2 is always empty and q1 is always not empty. push every time // Let the q2 push new element bring in the q1 element, so that the new element is the first element and can be operated public void push(int x) { q2.offer(x); while (!q1.isEmpty()){ q2.offer(q1.poll()); } Queue<Integer> temp=q1; q1=q2; q2=temp; } /** Removes the element on top of the stack and returns that element. */ public int pop() { return q1.poll(); } /** Get the top element. */ public int top() { return q1.peek(); } /** Returns whether the stack is empty. */ public boolean empty() { return q1.isEmpty(); }
Train of thought 2: is it OK to just let the incoming element as the header element of the queue? Since the inserted element can only be inserted from the tail of the element, can the element that meets this condition pop up and re insert the tail? Is it satisfied that the last element is at the head of the queue?
Solution 2:
Original queue:
[4 5 6 9 9]
Now to insert element 3:
a. Insert from the end of the queue normally
[4 5 6 9 9 3]
b. Pop up the elements of the original queue and insert them at the end of the queue
[5 6 9 9 3 4]
[6 9 9 3 4 5]
[9 9 3 4 5 6]
[9 3 4 5 6 9]
[3 4 5 6 9 9]
Does this satisfy the head of the element 3 queue of the new push?
Just pop it up and take it out!
Queue<Integer> q1; /** Initialize your data structure here. */ public MyStack() { q1=new LinkedList<>(); } //Use a queue. Every time you push, the head of the queue will pop out and be inserted into the end of the queue. Don't forget public void push(int x){ q1.offer(x); int size=q1.size(); while (size>1){ q1.add(q1.poll()); size--; } } /** Removes the element on top of the stack and returns that element. */ public int pop() { return q1.poll(); } /** Get the top element. */ public int top() { return q1.peek(); } /** Returns whether the stack is empty. */ public boolean empty() { return q1.isEmpty(); }
OK, if it's helpful, give me a compliment~