232.用栈实现队列
class MyQueue {
public:
stack<int>in;
stack<int>out;
MyQueue() {
}
void push(int x) {
in.push(x);
}
int pop() {
if(out.empty()){
while(!in.empty()){
out.push(in.top());
in.pop();
}
}
int a=out.top();
out.pop();
return a;
}
int peek() {
int ans=this->pop();
out.push(ans);
return ans;
}
bool empty() {
return in.empty()&&out.empty();
}
};
225. 用队列实现栈
class MyStack {
public:
queue<int> a;
MyStack() {}
void push(int x) { a.push(x); }
int pop() {
int len = a.size() - 1;
while (len > 0) {
a.push(a.front());
a.pop();
len--;
}
int z = a.front();
a.pop();
return z;
}
int top() { return a.back(); }
bool empty() { return a.empty(); }
};