leetcode?python 225. Implement Stack using Queues

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
       








































以上是关于leetcode?python 225. Implement Stack using Queues的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode225——用队列实现栈(python)

[leetcode]225. Implement Stack using Queues

leetcode225

leetcode-225-用队列实现栈

LeetCode OJ 225Implement Stack using Queues

[LeetCode] 225. 用队列实现栈