zoukankan      html  css  js  c++  java
  • leetcode232. 用栈实现队列

    使用栈实现队列的下列操作:

    push(x) -- 将一个元素放入队列的尾部。
    pop() -- 从队列首部移除元素。
    peek() -- 返回队列首部的元素。
    empty() -- 返回队列是否为空。
    示例:

    MyQueue queue = new MyQueue();

    queue.push(1);
    queue.push(2);
    queue.peek(); // 返回 1
    queue.pop(); // 返回 1
    queue.empty(); // 返回 false

     push操作搞一个辅助栈就好了

    python

    class MyQueue:
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.stack=[]
            self.help_stack=[]
    
        def push(self, x: int) -> None:
            """
            Push element x to the back of queue.
            """
            while self.stack:
                self.help_stack.append(self.stack.pop())
            self.help_stack.append(x)
            while self.help_stack:
                self.stack.append(self.help_stack.pop())
    
        def pop(self) -> int:
            """
            Removes the element from in front of queue and returns that element.
            """
            return self.stack.pop();
    
        def peek(self) -> int:
            """
            Get the front element.
            """
            return self.stack[-1]
    
    
        def empty(self) -> bool:
            """
            Returns whether the queue is empty.
            """
            return not bool(self.stack)
    
    
    # Your MyQueue object will be instantiated and called as such:
    # obj = MyQueue()
    # obj.push(x)
    # param_2 = obj.pop()
    # param_3 = obj.peek()
    # param_4 = obj.empty()

     Java

    class MyQueue {
        Stack<Integer>pushStack=new Stack<>();
        Stack<Integer>popStack=new Stack<>();
    
        /** Initialize your data structure here. */
        public MyQueue() {
            
        }
        
        /** Push element x to the back of queue. */
        public void push(int x) {
            while(!popStack.isEmpty()){
                pushStack.push(popStack.pop());
            }
            pushStack.push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            while(!pushStack.isEmpty()){
                popStack.push(pushStack.pop());
            }
            return popStack.pop();
        }
        
        /** Get the front element. */
        public int peek() {
             while(!pushStack.isEmpty()){
                popStack.push(pushStack.pop());
            }
            return popStack.peek();
        }
        
        /** Returns whether the queue is empty. */
        public boolean empty() {
            return pushStack.isEmpty()&&popStack.isEmpty();
        }
    }
    
    /**
     * Your MyQueue object will be instantiated and called as such:
     * MyQueue obj = new MyQueue();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.peek();
     * boolean param_4 = obj.empty();
     */
  • 相关阅读:
    SQL Server的数据库镜像实施笔记(3)
    MVC3缓存之三:MVC3中的局部缓存
    MVC3缓存之二:页面缓存中的局部动态
    SQL Server的数据库镜像实施笔记(2)
    ab工具
    数据库灾难备份,和负载均衡 主从数据库配置
    SQL Server的数据库镜像实施笔记 一
    ArcGIS version not specified. You must call RuntimeManager.
    SL命名空间书写
    通过文件tnsnames.ora连接
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12951180.html
Copyright © 2011-2022 走看看