剑指 Offer 09. 用两个栈实现队列
思路
用两个栈即可
代码
class CQueue {
Stack<Integer> st1;
Stack<Integer> st2;
public CQueue() {
st1=new Stack<>();
st2=new Stack<>();
}
public void appendTail(int value) {
st1.push(value);
}
public int deleteHead() {
if(st2.isEmpty()){
while(!st1.isEmpty()){
st2.push(st1.pop());
}
}
if(st2.isEmpty()){
return -1;
}
else{
return st2.pop();
}
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/