min_stack

  • 2022-12-14
  • 浏览 (555)

min_stack.py 源码

# 最小栈

class MinStack:

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

    def push(self, x: int) -> None:
        self.data.append(x)
        if len(self.helper) > 0:
            x = min(x, self.helper[-1])
        self.helper.append(x)

    def pop(self) -> None:
        self.data.pop()
        self.helper.pop()

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

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

你可能感兴趣的文章

largest_rectangle_histogram

next_greater_element_i

queue_use_stack

0  赞