# -*- coding:utf-8 -*-
class Solution:
def __init__(self) :
self.a = []
self.min_stack = []
def push(self, node):
# write code here
self.a.append(node)
#最小栈为空,后者输入值小于等与最小栈栈顶元素,则将输入值同时压入最小栈和数据栈中。
if not self.min_stack or node <= self.min_stack[-1] :
self.min_stack.append(node)
def pop(self):
# write code here
#如果弹出元素等于最小栈栈顶元素,则将最小栈栈顶元素一并弹出
if self.a.pop() == self.min_stack[-1] :
self.min_stack.pop()
def top(self):
# write code here
return self.a[-1]
#使用了最小栈,可以O(1)时间访问
def min(self):
# write code here
return self.min_stack[-1]