編寫一個(gè)類,只能用兩個(gè)棧結(jié)構(gòu)實(shí)現(xiàn)隊(duì)列,支持隊(duì)列的基本操作(push,pop)鼓蜒。
給定一個(gè)操作序列ope及它的長度n馍乙,其中元素為正數(shù)代表push操作耘分,為0代表pop操作佳镜,保證操作序列合法且一定含pop操作扇调,請(qǐng)返回pop的結(jié)果序列把兔。
測試樣例:
輸入:[1,2,3,0,4,0],6
返回:[1,2]
class TwoStack {
public:
stack<int> push_stack, pop_stack;
void pour_into(stack<int> &A, stack<int> &B)
{
while(!A.empty()){
B.push(A.top());
A.pop();
}
}
vector<int> twoStack(vector<int> ope, int n) {
vector<int> result;
for(int i=0; i<n; i++){
if(0 == ope[i]){
pour_into(push_stack, pop_stack);
result.push_back(pop_stack.top());
pop_stack.pop();
}else{
pour_into(pop_stack, push_stack);
push_stack.push(ope[i]);
}
}
return result;
}
};