class TwoStack {
stack<int>pushstack,popstack;
void push(int value){
while (!popstack.empty()) {
pushstack.push(popstack.top());
popstack.pop();
}
pushstack.push(value);
}
int pop(){
while (!pushstack.empty()) {
popstack.push(pushstack.top());
pushstack.pop();
}
int x = popstack.top();
popstack.pop();
return x;
}
public:
vector<int> twoStack(vector<int> ope, int n) {//操作序列,0为出队,非0入队.返回出队序列.
vector<int>ans;
for (int i=0; i<n; ++i) {
if(ope[i]==0)
ans.push_back(pop());
else
push(ope[i]);
}
return ans;
}
};
两个栈实现一个队列
最新推荐文章于 2024-04-22 23:20:09 发布