class MyStack {
public:
MyStack() {
}
void push(int x) {
q.push(x);
}
int pop() {
int size = q.size();
size--;
while(size--) {
q.push(q.front());
q.pop();
}
int tmp = q.front();
q.pop();
return tmp;
}
int top() {
return q.back();
}
bool empty() {
if(q.empty()) return true;
else return false;
}
private:
queue<int> q;
};
/**
* 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();
*/
力扣225. 用队列实现栈
最新推荐文章于 2025-05-22 10:04:48 发布