155. Min Stack

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了155. Min Stack相关的知识,希望对你有一定的参考价值。

155. Min Stack

#!/usr/bin/env python
class MinStack(object):

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

    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        self.stack.append(x)
        if not self.minStack:
            self.minStack.append(x)
        else:
            top = self.minStack[-1]
            if top >= x:
                self.minStack.append(x)
    #
    def pop(self):
        """
        :rtype: void
        """
        if self.stack:
            x = self.stack.pop()
            top = self.minStack[-1]
            if x == top:
                return self.minStack.pop()

    def top(self):
        """
        :rtype: int
        """
        if self.stack:
            return self.stack[-1]

    def getMin(self):
        """
        :rtype: int
        """
        if self.minStack:
            return self.minStack[-1]


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

以上是关于155. Min Stack的主要内容,如果未能解决你的问题,请参考以下文章

155. Min Stack

#Leetcode# 155. Min Stack

155. Min Stack - Unsolved

155. Min Stack

Leetcode 155. Min Stack

155. Min Stack