728x90
class MyQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
//enqueue (FIFO)
public void push(int x) {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
stack1.push(x);
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
}
//dequeue (FIFO)
public int pop() {
if(empty()){
return 0;
}else{
return stack1.pop();
}
}
public int peek() {
return stack1.peek();
}
public boolean empty() {
return stack1.isEmpty();
}
}
https://leetcode.com/problems/implement-queue-using-stacks/
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] #20. Valid Parentheses (0) | 2024.02.08 |
---|---|
[Leetcode] #14. Longest Common Prefix (repeat : 0 ) (0) | 2024.01.31 |
[LeetCode] #58. Length of Last Word (repeat : 0) (2) | 2024.01.23 |
[LeetCode]#13. Roman to Integer (repeat : 0 ) (0) | 2024.01.22 |
[LeetCode] #169. Majority Element (repeat : 0 ) (0) | 2024.01.21 |