题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
# -*- coding:utf-8 -*- class Solution: def __init__(self) -> None: self.s1=[] self.s2=[] def push(self, node): if not self.s2: self.s2.append(node) self.s1.append(node) if len(self.s2)>0 and self.s2[-1]<=node: self.s2.append(self.s2[-1]) if len(self.s2)>0 and self.s2[-1]>node: self.s2.append(node) # write code here def pop(self): self.s1.pop() self.s2.pop() # write code here def top(self): # write code herE if self.s1: return self.s1[-1] else: return None def min(self): if self.s2: return self.s2[-1] else: return None # write code here