剑指Offer 30 包含min函数的栈

包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def __init__(self):
 4         self.stack = []
 5         self.list = []
 6         
 7     def push(self, node):
 8         self.stack.append(node)
 9         self.list.append(node)
10         self.list = sorted(self.list)
11         # write code here
12     def pop(self):
13         r = self.stack.pop(-1)
14         self.list.remove(r)
15         return r
16         # write code here
17     def top(self):
18         r = self.stack[-1]
19         return r
20         # write code here
21     def min(self):
22         r = self.list[0]
23         return r
24         # write code here
原文地址:https://www.cnblogs.com/asenyang/p/11013875.html