剑指 Offer 30. 包含min函数的栈
https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
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 not self.minstack:
self.minstack.append(x)
elif self.minstack[-1]<x:
self.minstack.append(self.minstack[-1])
else:
self.minstack.append(x)
def pop(self):
"""
:rtype: None
"""
if self.stack:
self.stack.pop(-1)
self.minstack.pop(-1)
def top(self):
"""
:rtype: int
"""
if self.stack:
return self.stack[-1]
def min(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.min()