1. 思路
时间复杂度要求为O(1) ,那我们就从空间复杂度入手
2. 代码
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stk = []
self.stk_min = []
def push(self, node):
# write code here
self.stk.append(node)
if self.stk_min == []:
self.stk_min.append(node)
else:
self.stk_min.append(min(node,self.stk_min[-1]))
def pop(self):
# write code here
self.stk.pop()
self.stk_min.pop()
def top(self):
# write code here
return self.stk[-1]
def min(self):
# write code here
return self.stk_min[-1]