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

    题目描述

    定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
     
    # -*- coding:utf-8 -*-
    class Solution:
        def __init__(self):
            self.stack = []
            self.minstack = []
        def push(self, node):
            # write code here
            if not self.minstack or self.minstack[-1] >= node:
                self.minstack.append(node)
            self.stack.append(node)
        def pop(self):
            # write code here
            if self.stack[-1]  == self.minstack[-1]:
                self.minstack.pop()
            self.stack.pop() 
        def top(self):
            # write code here
            return self.stack[-1]
        def min(self):
            # write code here
            return self.minstack[-1]
  • 相关阅读:
    adjacent_diffenerce
    数值算法速食食谱
    accumulate
    平面分割
    进制转换
    奖学金
    谁考了第k名
    奇数单增序列
    病人排序
    灯的开关状态
  • 原文地址:https://www.cnblogs.com/ansang/p/11993965.html
Copyright © 2011-2022 走看看