AcWing 20. 用两个栈实现队列
原题链接
简单
作者:
模板菜鸡
,
2021-04-27 21:19:16
,
所有人可见
,
阅读 302
class MyQueue {
/** Initialize your data structure here. */
Stack<Integer> stk1;
Stack<Integer> stk2;
public MyQueue() {
stk1=new Stack<>();
stk2=new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stk1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stk1.isEmpty()&&stk2.isEmpty()){
return 0;
}else if(stk2.isEmpty()){
while(!stk1.isEmpty()){
stk2.push(stk1.pop());
}
}
return stk2.pop();
}
/** Get the front element. */
public int peek() {
if(stk1.isEmpty()&&stk2.isEmpty()){
return 0;
}else if(stk2.isEmpty()){
while(!stk1.isEmpty()){
stk2.push(stk1.pop());
}
}
return stk2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
if(stk1.isEmpty()&&stk2.isEmpty()) return true;
return false;
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/