码迷,mamicode.com
首页 > 编程语言 > 详细

【leetcode?python】 225. Implement Stack using Queues

时间:2016-10-31 20:38:44      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:structure   while   python   initial   bsp   先进后出   type   stack   self   

#栈是先进后出

#队列先进先出

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
       

【leetcode?python】 225. Implement Stack using Queues

标签:structure   while   python   initial   bsp   先进后出   type   stack   self   

原文地址:http://www.cnblogs.com/kwangeline/p/6016918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!