zoukankan      html  css  js  c++  java
  • python剑指offer 包含min函数的栈

    题目描述

    定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
    # -*- coding:utf-8 -*-
    class Solution:
        def __init__(self):
            self.stack = []
            self.min_stack = []
        def push(self, node):
            # write code here
            self.stack.append(node)
            if not self.min_stack or node <= self.min_stack[-1]:
                self.min_stack.append(node)
            else:
                self.min_stack.append(self.min_stack[-1])
        def pop(self):
            # write code here
            #if not self.stack:
            self.stack.pop()
            self.min_stack.pop()
        def top(self):
            # write code here
            return self.stack[-1]
        def min(self):
            # write code here
            return self.min_stack[-1]
    
  • 相关阅读:
    LOJ#551 Matrix
    洛谷P5163 WD与地图
    洛谷P4831 Scarlet loves WenHuaKe
    LOJ#6118 鬼牌
    支配树
    线性插值
    PropertiesConfiguration
    ThreadLocal
    Thread
    FastJson
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/9685649.html
Copyright © 2011-2022 走看看