LeetCode学习之-225. 利用队列实现堆栈(Implement Stack using Queues)

来源:互联网 发布:淘宝洋垃圾服装 编辑:程序博客网 时间:2024/06/06 01:25

1.算法

[1]

2.代码

[2]

"""Author: Tianze TangDate: 2017-07-17Email:454779758@qq.comFunction: Use two queue to realize stack's function.Explain:Modified Log:"""class MyStack(object):    def __init__(self):        """        Initialize your data structure here.        """        self.Queue= []    def push(self,x):        self.Queue.append(x)    def pop(self):        return(self.Queue.pop())    def empty(self):        return not self.Queue    def top(self):        return(self.Queue[-1])a = MyStack()a.push(1)print(a.Queue)a.push(2)print(a.Queue)print(a.top())print(a.top())

3.输出

[1]
[1, 2]
2
2

4.参考资料

[1] 《算法导论》第3版 :第10章 基本数据结构 10.1栈和列队
[2] 225. Implement Stack using Queues

阅读全文
0 0
原创粉丝点击