题目:
class CQueue {
public:
stack<int> st1;
stack<int> st2;
CQueue() {
}
void appendTail(int value) {
st1.push(value);
}
int deleteHead() {
if(st1.empty()&&st2.empty())return -1;
if(!st2.empty()){ //要先从st2中弹出队列头部元素
int top=st2.top();
st2.pop();
return top;
}
while(!st1.empty()){ //st2为空的时候再把st1中元素全部导入st2中,此后再弹出队列头部元素
st2.push(st1.top());
st1.pop();
}
int top=st2.top();
st2.pop();
return top;
}
};
标签:Offer,队列,top,09,int,st1,st2,empty
From: https://www.cnblogs.com/fly-smart/p/17586047.html