当前位置 博文首页 > 算法探索之路:LeetCode 225. 用队列实现栈 Python3实现
【原题连接】
代码如下:
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.__list = []
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.__list.append(x)
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
return self.__list.pop()
def top(self) -> int:
"""
Get the top element.
"""
return self.__list[-1]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return self.__list == []
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
cs