LeetCode简单题:225. 用队列实现栈(Python,C++,Java)

一.解法

https://leetcode-cn.com/problems/implement-stack-using-queues/
要点:队列
Python,C++,Java都用了单队列模拟栈,关键在于push新元素之后要将前面所有的元素pop然后重新push到队尾让新元素到第一个pop位置。

二.Python实现

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.q = []

    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.q.append(x)
        q_length = len(self.q)
        while q_length > 1:
            self.q.append(self.q.pop(0)) #反转前n-1个元素,栈顶元素始终保留在队首
            q_length -= 1

    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.q.pop(0)

    def top(self) -> int:
        """
        Get the top element.
        """
        return self.q[0]


    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return not bool(self.q)

# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

三.C++实现

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    queue <int> a;
    
    /** Push element x onto stack. */
    void push(int x) {
        a.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        for(int i=0;i<a.size()-1;i++)
        {a.push(a.front());a.pop();}
        int temp=a.front();
        a.pop();
        return temp;
    }
    
    /** Get the top element. */
    int top() {
         for(int i=0;i<a.size()-1;i++)
        {a.push(a.front());a.pop();}
        int temp=a.front();
        a.push(a.front());
        a.pop();
        return temp;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return a.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();
 */

四.java实现

class MyStack {

    /** Initialize your data structure here. */
    public MyStack() {
        
    }

    private LinkedList<Integer> q1 = new LinkedList<>();

    
    /** Push element x onto stack. */
    public void push(int x) {
        q1.add(x);
        int sz = q1.size();
        while (sz > 1) {
            q1.add(q1.remove());
            sz--;
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return q1.remove();
    }
    
    /** Get the top element. */
    public int top() {
       return q1.peek();

    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        
        return q1.isEmpty();

    }
}

/**
 * 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();
 * boolean param_4 = obj.empty();
 */
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。