标签:param leetcode import ISE 图片 lan href pen pre
把数据在两个栈中分别“折腾”一次,出来的顺序就是队列的顺序。
向stack1中push数据——向队列中添加数据
从stack2中弹出数据——从队列中删除头部数据
import java.util.Stack;
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(!stack2.isEmpty()){
return stack2.pop();
}
if(!stack1.isEmpty()){
while(!stack1.isEmpty()){
int temp = stack1.pop();
stack2.push(temp);
}
return stack2.pop();
}
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
标签:param leetcode import ISE 图片 lan href pen pre
原文地址:https://www.cnblogs.com/realzhaijiayu/p/13222158.html