最小栈

描述

155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

思路

使用辅助栈存储原始栈每个状态的最小值。辅助栈中大部分值是相同的,有没有更优的做法呢?

解答

class MinStack:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self._stack = []
        self._min_stack = []


    def push(self, x: int) -> None:
        self._stack.append(x)
        if not self._min_stack:
            min_ = x
        else:
            min_ = min(x, self._min_stack[-1])
        self._min_stack.append(min_)

    def pop(self) -> None:
        self._stack.pop()
        self._min_stack.pop()

    def top(self) -> int:
        return self._stack[-1]

    def getMin(self) -> int:
        return self._min_stack[-1]

标签: Easy