zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 225. Implement Stack using Queues

    #栈是先进后出

    #队列先进先出

    class Stack(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.inQueue=[]
            self.outQueue=[]

        def push(self, x):
            """
            :type x: int
            :rtype: nothing
            """
            self.inQueue.append(x)
            
            

        def pop(self):
            """
            :rtype: nothing
            """
            i=0
            while i<len(self.inQueue)-1:
                self.outQueue.append(self.inQueue[i])
                i+=1
            self.inQueue=self.outQueue
            self.outQueue=[]
           
           
            
        def top(self):
            """
            :rtype: int
            """
            tmpQueue=self.inQueue
            i=0;
            while i<(len(self.inQueue)-1):
                self.outQueue.append(self.inQueue[i])
                i+=1
            
            res=[i for i in self.inQueue if i not in self.outQueue]
            self.outQueue=[]
            return res[0]
            

        def empty(self):
            """
            :rtype: bool
            """
            return True if (len(self.inQueue))==0 else False
           

  • 相关阅读:
    c#将 1, 2, ..., 9共 9 个数字分成 3 组
    信息学院本科生创新项目总结
    Element-ui的使用
    fastmock接口管理
    mock安装与使用
    开闭原则
    里氏替换原则
    依赖倒置原则
    接口隔离原则
    单一职责原则
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6016918.html
Copyright © 2011-2022 走看看