image_2022-02-07_23-43-26.png
55.5 KB
#N232. Implement Queue using Stacks
problem link
#solution
problem link
#solution
class MyQueue {
Stack<Integer> stack = new Stack();
Stack<Integer> temp = new Stack();
public MyQueue() {}
public void push(int x) {
while(!stack.isEmpty())
temp.push(stack.pop());
stack.push(x);
while(!temp.isEmpty())
stack.push(temp.pop());
}
public int pop() {
return stack.pop();
}
public int peek() {
return stack.peek();
}
public boolean empty() {
return stack.isEmpty();
}
}