编写一个类,只能用两个栈结构实现队列,支持队列的基本操作(push,pop)。
给定一个操作序列ope及它的长度n,其中元素为正数代表push操作,为0代表pop操作,保证操作序列合法且一定含pop操作,请返回pop的结果序列。
class TwoStack {
public:
stack<int> s1;
stack<int> s2;
vector<int> twoStack(vector<int> ope, int n) {
// write code here
int i;
int all=0;
vector<int> result;
for(i=0;i<n;i++)
{
if(ope[i]>0)
{
s1.push(ope[i]);
}
else
{
all++;
}
}
while(!s1.empty())
{
int temp=s1.top();
s1.pop();
s2.push(temp);
}
while(!s2.empty()&&all)
{
int temp=s2.top();
result.push_back(temp);
s2.pop();
all--;
}
return result;
}
};