155.Min Stack

 

 

class MinStack:

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

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

    def pop(self) -> None:
        self.stack.pop()

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

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

 

posted @ 2020-05-09 14:56  星海寻梦233  阅读(63)  评论(0编辑  收藏  举报