美文网首页
2019-08-24LeetCode155. 最小栈

2019-08-24LeetCode155. 最小栈

作者: mztkenan | 来源:发表于2019-08-24 17:09 被阅读0次

    5min,一次过,爽啊

    class MinStack:
    
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.store=[]
            self.min_store=[]
    
        def push(self, x: int) -> None:
            self.store.append(x)
            if not self.min_store:self.min_store.append(x)
            elif x<self.min_store[-1]:self.min_store.append(x)
            elif x>=self.min_store[-1]:self.min_store.append(self.min_store[-1])
    
        def pop(self) -> None:
            self.store.pop()
            self.min_store.pop()
    
        def top(self) -> int:
            return self.store[-1]
    
        def getMin(self) -> int:
            return self.min_store[-1]
    

    相关文章

      网友评论

          本文标题:2019-08-24LeetCode155. 最小栈

          本文链接:https://www.haomeiwen.com/subject/dvyqectx.html