1. 程式人生 > >劍指offer20.包含min函式的棧

劍指offer20.包含min函式的棧

https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49?tpId=13&tqId=11173&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking

題目描述
定義棧的資料結構,請在該型別中實現一個能夠得到棧中所含最小元素的min函式(時間複雜度應為O(1))。

# -*- coding:utf-8 -*-
class Solution:
    def
__init__(self): self.stack = [] def push(self, node): # write code here self.stack.append(node) def pop(self): # write code here if len(self.stack) == 0: return False return self.stack.pop(-1) def top(self): # write code here
return self.stack[-1] def min(self): # write code here return min(self.stack)