题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
注意:保证测试中不会当栈为空的时候,对栈调用pop()或者min()或者top()方法。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.s_stack = []
self.min_stack = []
def push(self, node):
self.s_stack.append(node)
if self.min_stack:
if self.min_stack[-1] > node:
self.min_stack.append(node)
else:
self.min_stack.append(self.min_stack[-1])
else:
self.min_stack.append(node)
# write code here
def pop(self):
# write code here
if self.s_stack:
self.s_stack.pop()
self.min_stack.pop()
def top(self):
# write code here
if self.s_stack:
return self.s_stack[-1]
if self.min_stack:
return self.min_stack[-1]
def min(self):
# write code here
if self.min_stack:
return self.min_stack[-1]
return None
769

被折叠的 条评论
为什么被折叠?



