劍指offer_包含min函式的棧
阿新 • • 發佈:2018-11-30
題目描述
定義棧的資料結構,請在該型別中實現一個能夠得到棧中所含最小元素的min函式(時間複雜度應為O(1))。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, node):
# write code here
if not self.min_stack or node < self.min_stack[-1]:
self.min_stack.append(node)
self.stack.append(node)
def pop(self):
# write code here
if self.stack[-1]==self.min_stack[-1]:
self.min_stack.pop()
return self.stack.pop()
def top(self):
# write code here
return self.stack[-1]
def min(self):
# write code here
return self.min_stack[-1]