码迷,mamicode.com
首页 > 其他好文 > 详细

232 Implement Queue using Stacks

时间:2015-07-17 09:36:59      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

232 Implement Queue using Stacks

用2个stack 完成 代码如下

class Queue:
    # initialize your data structure here.
    def __init__(self):
        self.stackA = []
        self.stackB = []

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

    # @return nothing
    def pop(self):
        while self.stackA != []:
            self.stackB.append(self.stackA.pop())
        self.stackB.pop()
        while self.stackB != []:
            self.stackA.append(self.stackB.pop())

    # @return an integer
    def peek(self):
        while self.stackA != []:
            self.stackB.append(self.stackA.pop())
        x = self.stackB[-1]
        while self.stackB != []:
            self.stackA.append(self.stackB.pop())
        return x

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

 

232 Implement Queue using Stacks

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4653436.html

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