• 009.栈实现队列


    class MyQueue(object):
    
        def __init__(self, ):
            """
            Initialize your data structure here.
            """
            self.instack = []
            self.outstack = []
    
        def push(self, x):
            """
            Push element x to the back of queue.
            :type x: int
            :rtype: void
            """
            self.instack.append(x)
    
        def pop(self):
            """
            Removes the element from in front of queue and returns that element.
            :rtype: int
            """
            if not self.outstack:
                if not self.instack:
                    return
                else:
                    while self.instack:
                        node = self.instack.pop()
                        self.outstack.append(node)
            return self.outstack.pop()
    
        def peek(self):
            """
            Get the front element.
            :rtype: int
            """
            if not self.outstack:
                if not self.instack:
                    return
                else:
                    while self.instack:
                        node = self.instack.pop()
                        self.outstack.append(node)
            return self.outstack[-1]
    
        def empty(self):
            """
            Returns whether the queue is empty.
            :rtype: bool
            """
            if len(self.instack) == 0 and len(self.outstack) == 0:
                return True
            else:
                return False
    
    # 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()
    

      

  • 相关阅读:
    idea输出目录详解
    svn的使用教程
    java常用技术名词解析
    1.0 idea使用教程(配置)一
    fastDFS的搭建
    log4j的配置
    关于elementUI中上传组件点击上传时页面卡死的问题
    Nginx的反向代理
    给所有实体类重写tostring方法
    Nginx的配置
  • 原文地址:https://www.cnblogs.com/wanyp/p/10065602.html
Copyright © 2020-2023  润新知