zoukankan      html  css  js  c++  java
  • [LC] 225. Implement Stack using Queues

    Implement the following operations of a stack using queues.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • empty() -- Return whether the stack is empty.

    Example:

    MyStack stack = new MyStack();
    
    stack.push(1);
    stack.push(2);  
    stack.top();   // returns 2
    stack.pop();   // returns 2
    stack.empty(); // returns false

    class MyStack:
        
        def __init__(self):
            """
            Initialize your data structure here.
            """
            from collections import deque
            self.queue = deque()
    
        def push(self, x: int) -> None:
            """
            Push element x onto stack.
            """
            self.queue.append(x)
            for i in range(len(self.queue) - 1):
                self.queue.append(self.queue.popleft())
            
    
        def pop(self) -> int:
            """
            Removes the element on top of the stack and returns that element.
            """
            return self.queue.popleft()
    
        def top(self) -> int:
            """
            Get the top element.
            """
            return self.queue[0]
    
        def empty(self) -> bool:
            """
            Returns whether the stack is empty.
            """
            return not self.queue
    
    
    # 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()
    class MyStack {
    
        /** Initialize your data structure here. */
        Queue<Integer> queue;
        public MyStack() {
            queue = new LinkedList<>();
        }
        
        /** Push element x onto stack. */
        public void push(int x) {
            queue.offer(x);
            for (int i = 0; i < queue.size() - 1; i++) {
                queue.offer(queue.poll());
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            return queue.poll();
        }
        
        /** Get the top element. */
        public int top() {
            return queue.peek();
        }
        
        /** Returns whether the stack is empty. */
        public boolean empty() {
            return queue.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();
     */
  • 相关阅读:
    Dom对象和jQuery对象区别 jQuery对象转换为Dom对象、、Dom对象转换为jquery对象
    jquery 1,2,3三个版本的下载、区别/以及jquery使用步骤,jQuery入口函数
    2021年3月4日 第一周开课博客
    2021年3月3日
    2021年3月2日
    2021年2月24日 记账本开发07
    2021年2月23日 记账本开发06
    2021年2月22日 记账本开发05
    程序员修炼之道读书笔记03
    2021年2月21日 记账本开发04
  • 原文地址:https://www.cnblogs.com/xuanlu/p/11781045.html
Copyright © 2011-2022 走看看