思路: 在add new item 之後將 除新item之後的數都加進queues中 e.g. 12--> 21 , add 3 --> 321 , add 4--> 4321
class MyStack {
Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
this.queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue.add(x);
int size = queue.size();
while(size>1){
queue.add(queue.poll());
size--;
}
}
/** 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();
}
}