#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <exception>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
stack<int> stk1,stk2;
int n;
void Push(int tmp){
stk1.push(tmp);
}
int Pop(){
int tmp;
if(stk2.size()<=0){
while(stk1.size()>0){
tmp=stk1.top();
stk1.pop();
stk2.push(tmp);
}
}
if(stk2.size()==0) return -1;
tmp=stk2.top();
stk2.pop();
return tmp;
}
int main()
{
cin>>n;
string s;
int tmp;
while(n--){
cin>>s;
if(s=="PUSH"){
cin>>tmp;
Push(tmp);
}
else{
printf("%d\n",Pop());
}
}
return 0;
}
剑指offer :07 用两个栈实现队列
最新推荐文章于 2022-08-29 10:32:31 发布