[Algorithms] LeetCode 225. 用队列实现栈
Categories Algorithms StackAndQueue
Tags
来源:代码随想录
单个队列模拟栈
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int size = que.size() - 1;
while (size--) {
que.push(que.front());
que.pop();
}
int a = que.front();
que.pop();
return a;
}
int top() {
int a = this->pop();
this->push(a);
return a;
}
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();
*/
Page views: Loading... · Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号