【leetcode❤python】 155. Min Stack

#-*- coding: UTF-8 -*-
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 len(self.minStack)==0 or x<=self.minStack[-1]:
            self.minStack.append(x)

    def pop(self):
        """
        :rtype: void
        """
        if self.Stack!=None:
            if self.top()==self.minStack[-1]:
                self.minStack.pop()
            self.Stack.pop()
        

    def top(self):
        """
        :rtype: int
        """
        if self.Stack!=None:
            return self.Stack[-1]

    def getMin(self):
        """
        :rtype: int
        """
        if self.minStack!=None:
            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()

posted @ 2016-11-13 19:28  火金队长  阅读(192)  评论(0编辑  收藏  举报