用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入: ["CQueue","appendTail","deleteHead","deleteHead"] [[],[3],[],[]] 输出:[null,null,3,-1]
class CQueue {
private Stack
s1; private Stack
s2; public CQueue() {
s1=new Stack<>();
s2=new Stack<>();
}
public void appendTail(int value) {
s1.push(value);
}
public int deleteHead() {
int ret = 0;
if(s1.empty()){
return -1;
}
while(!s1.empty()){
ret=s1.pop();
s2.push(ret);
}
s2.pop();
while(!s2.empty()){
s1.push(s2.pop());
}
return ret;
}
}