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

1.算法

[1]

2.代码

[2]

"""
Author: Tianze Tang
Date: 2017-07-17
Email:[email protected]
Function: 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

你可能感兴趣的:(Python,LeetCode)