225. Implement Stack using Queues
难度:Easy
- 用 C++ 编写以便直接使用 queue 容器
- 做法还是容易想到的
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int sz = que.size();
for (int i=1; i<sz; ++i)
{
que.push(que.front());
que.pop();
}
int ret = que.front();
que.pop();
return ret;
}
int top() {
int sz = que.size();
for (int i=1; i<sz; ++i)
{
que.push(que.front());
que.pop();
}
int ret = que.front();
que.push(que.front());
que.pop();
return ret;
}
bool empty() {
return que.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/