LeetCode 155. 最小栈 python3 最简单的解题

it2025-05-06  8

LeetCode

原题思路python3最简单的解法

原题

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 x 推入栈中。 pop() -- 删除栈顶的元素。 top() -- 获取栈顶元素。 getMin() -- 检索栈中的最小元素。 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/min-stack 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

以快速开发为目的来进行的解题, 导致时间和内存占比都很大

python3最简单的解法

class MinStack: def __init__(self): """ initialize your data structure here. """ self.l_list = [] def push(self, x: int) -> None: self.l_list.append(x) def pop(self) -> None: self.l_list.pop() def top(self) -> int: return self.l_list[-1] def getMin(self) -> int: return min(self.l_list) # Your MinStack object will be instantiated and called as such: # obj = MinStack() # obj.push(x) # obj.pop() # param_3 = obj.top() # param_4 = obj.getMin()
最新回复(0)