Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-02-07_23-43-26.png
55.5 KB
#N232. Implement Queue using Stacks
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();
}
}