美文网首页
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. 最小栈

    5min,一次过,爽啊

  • LeetCode-155-最小栈

    LeetCode-155-最小栈 155. 最小栈[https://leetcode-cn.com/problem...

  • 最小栈

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 ...

  • 最小栈

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 ...

  • 最小栈

    https://leetcode-cn.com/explore/interview/card/bytedance/...

  • 最小栈

    题目描述 https://leetcode-cn.com/problems/min-stack/ 解 思路 用一个...

  • 最小栈

    题目 难度级别:简单 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 pu...

  • 最小栈

    题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) --...

  • 最小栈

    题目:MinStack minStack = new MinStack();minStack.push(-2);m...

  • 最小栈

    题目信息 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) ...

网友评论

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

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