一:题目
二:上码
class MyStack {
private:/**思路:1.我们每次push的时候 先push进队列,然后的话,将该元素前面的元素都出队重新push进该队列的尾部*/queue<int>q;
public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {int size = q.size();//push x 元素前的容器的大小q.push(x);while (size--) {int nums = q.front();q.pop();q.push(nums);}}/** Removes the element on top of the stack and returns that element. */int pop() {int nums = q.front();q.pop();return nums;}/** Get the top element. */int top() {int nums = q.front();return nums;}/** Returns whether the stack is empty. */bool empty() {return q.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();*/