思路:
一个栈用来辅助,另一个用来维护队列的值。
代码:
class CQueue {
public:
CQueue() {
}
void appendTail(int value) {
stk1.push(value);
}
int deleteHead() {
if(stk1.empty()) return -1;
while(stk1.size() != 1){
stk2.push(stk1.top());
stk1.pop();
}
int res = stk1.top();
stk1.pop();
while(stk2.size()) {
stk1.push(stk2.top());
stk2.pop();
}
return res;
}
stack<int> stk1;
stack<int> stk2;
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
评论区