225 Implement Stack using Queues

这道题一般既可以用一个queue也可以用2个queue来解决 这里使用一个queue来解决 代码如下

class Stack:
    # initialize your data structure here.
    def __init__(self):
        self.stack = []

    # @param x, an integer
    # @return nothing
    def push(self, x):
        self.stack.append(x)

    # @return nothing
    def pop(self):
        for i in range(0,len(self.stack) - 1):
            self.stack.append(self.stack.pop(0))
        self.stack.pop(0)

    # @return an integer
    def top(self):
        return self.stack[-1]

    # @return an boolean
    def empty(self):
        return self.stack == []

 

posted @ 2015-07-07 05:14  dapanshe  阅读(106)  评论(0编辑  收藏  举报