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
           

  • 相关阅读:
    算法面试准备(一)之----交叉熵与logistic回归推导
    Julia初学备忘
    二维数组中的查找,替换空格
    快慢指针倒数第K个节点,每K个一组反转链表
    贝叶斯网络之----(d-分离步骤)
    一笑
    尾曲
    ggplot在python中的使用(plotnine)
    SVC之SMO算法理解
    特征选取之IV(信息值)及python实现
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6016918.html
Copyright © 2011-2022 走看看