请设计一个栈,除了常规栈支持的pop与push函数以外,还支持min函数,该函数返回栈元素中的最小值。执行push、pop和min操作的时间复杂度必须为O(1)。
示例:
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-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
用min_s做辅助栈
class MinStack: def __init__(self): """ initialize your data structure here. """ self.s=[] self.min_s=[] def push(self, x: int) -> None: self.s.append(x) if not self.min_s or x<=self.min_s[-1]: self.min_s.append(x) def pop(self) -> None: if self.s.pop()==self.min_s[-1]: self.min_s.pop() def top(self) -> int: return self.s[-1] def getMin(self) -> int: return self.min_s[-1] # 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()