leetcode-155.-最小栈(python)

    1. 最小栈
        设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
        push(x) -- 将元素 x 推入栈中。
        pop() -- 删除栈顶的元素。
        top() -- 获取栈顶元素。
        getMin() -- 检索栈中的最小元素。
    
  • Python语言:使用辅助栈记录小的元素

    class MinStack:
    
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack=[]
            self.minstack=[]
            self.length = 0
    
        def push(self, x: int) -> None:
            self.stack.append(x)
    
            if self.length == 0:
                self.minstack.append(x)
            elif x < self.minstack[-1]:
                self.minstack.append(x)
            else:
                self.minstack.append(self.minstack[-1])
            self.length += 1
    
        def pop(self) -> None:
            if self.length == 0:
                return False
            self.stack.pop()
            self.minstack.pop()
            self.length -=1
    
        def top(self) -> int:
            return self.stack[-1]
    
        def getMin(self) -> int:
            return self.minstack[-1]
    

仙衣眠云碧岚袍,一襟潇洒,两袖飘飘。玉墨舒心春酝瓢,行也逍遥,坐也逍遥。
原文地址:https://www.cnblogs.com/max520liuhu/p/11009980.html