Python Leetcode 155. Min Stack
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minstack = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stack.append(x)
if len(self.minstack)==0 or x<self.minstack[-1]:
self.minstack.append(x)
else:
self.minstack.append(self.minstack[-1])
def pop(self):
"""
:rtype: None
"""
self.stack.pop()
self.minstack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.minstack[-1] if self.minstack else None