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
           

  • 相关阅读:
    mysql索引
    mysql视图
    pymysql
    web前端基础
    【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)
    【BZOJ2730】[HNOI2012] 矿场搭建(找割点)
    网络流(一)——最大流
    欧拉路与欧拉回路
    扫描线(一)——求矩形面积并
    【洛谷3396】哈希冲突(大力分块)
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6016918.html
Copyright © 2011-2022 走看看