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

    #-*- coding: UTF-8 -*-
    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

    sol=Stack()
    sol.push(1)
    sol.push(2)
    print sol.top()
    sol.pop()
    print sol.top()
    sol.pop()
    print sol.empty()

  • 相关阅读:
    【13NOIP提高组】货车运输(洛谷P1967)(Acwing.506)(一本通1877)
    Acwing136. 邻值查找(《算法竞赛进阶指南》)
    python 万能时间格式转换
    解决高并发的方法
    2020年春季学期《软件工程》课程总结
    语文知识点
    地理知识点
    外接球问题
    人船模型
    动量定理流体问题
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6059559.html
Copyright © 2011-2022 走看看