题解 | #包含min函数的栈#

包含min函数的栈

http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

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

这样的时间复杂度是O(n)不满足要求,看了下题解,在new一个list存最小值即可。

全部评论

相关推荐

bLanK的小号:建议自己写一个比较新颖的项目,比如思维导图,在线文档,仿造postman,仿造一个组件库
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务