Leetcode 剑指 Offer 30题参考代码如下:
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.queue=[]
def push(self, x: int) -> None:
self.queue.insert(0,x)
def pop(self) -> None:
self.queue.pop(0)
def top(self) -> int:
return self.queue[0]
def min(self) -> int:
return min(self.queue)
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.min()
题目链接:
力扣
https://leetcode.cn/problems/bao-han-minhan-shu-de-zhan-lcof
本文介绍了如何使用Python实现LeetCode中的剑指Offer 30题——最小栈,通过队列数据结构实现既能保持栈的基本操作,又能快速获取栈中的最小值。代码实例及详细解析有助于理解栈和队列在问题中的巧妙应用。
266

被折叠的 条评论
为什么被折叠?



