(栈)Leetcode155最小栈+739每日温度

739. 每日温度 - 力扣(LeetCode)

while要把stack的判断放在前面,否则stack[-1]可能报错

python 复制代码
class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        #单调栈里存元素的话还要返回去得下标
        #直接存下标就没有这个问题
        #单调栈存放遍历过但没结果的数
        ans=[0]*len(temperatures)
        stack=[]
        for i in range(len(temperatures)):
            if not stack:
                stack.append(i)
            elif temperatures[i]>temperatures[stack[-1]]:
                #while temperatures[i]>temperatures[stack[-1]] and stack:
                while stack and temperatures[i]>temperatures[stack[-1]]:
                    ans[stack[-1]]=i-stack[-1]
                    stack.pop()
                stack.append(i)
            else:
                stack.append(i)
        return ans

155. 最小栈 - 力扣(LeetCode)

双栈,空间换时间,单独维护一个最小栈,最小栈每一个位置对应栈那个位置的最小值

python 复制代码
class MinStack(object):

    def __init__(self):
        #minstack栈顶是维护和stack相同长度目前为止最小的元素
        self.stack=[]
        self.minstack=[]

    def push(self, val):
        """
        :type val: int
        :rtype: None
        """
        if not self.minstack:
            self.minstack.append(val)
        else:
            self.minstack.append(min(self.minstack[-1],val))
        self.stack.append(val)
    def pop(self):
        """
        :rtype: None
        """
        self.stack.pop()
        self.minstack.pop()
        

    def top(self):
        """
        :rtype: int
        """

        return self.stack[-1]

    def getMin(self):
        """
        :rtype: int
        """
        return self.minstack[-1]
        

739. 每日温度 - 力扣(LeetCode)

while要把stack的判断放在前面,否则stack[-1]可能报错

python 复制代码
class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        #单调栈里存元素的话还要返回去得下标
        #直接存下标就没有这个问题
        #单调栈存放遍历过但没结果的数
        ans=[0]*len(temperatures)
        stack=[]
        for i in range(len(temperatures)):
            if not stack:
                stack.append(i)
            elif temperatures[i]>temperatures[stack[-1]]:
                #while temperatures[i]>temperatures[stack[-1]] and stack:
                while stack and temperatures[i]>temperatures[stack[-1]]:
                    ans[stack[-1]]=i-stack[-1]
                    stack.pop()
                stack.append(i)
            else:
                stack.append(i)
        return ans

155. 最小栈 - 力扣(LeetCode)

双栈,空间换时间,单独维护一个最小栈,最小栈每一个位置对应栈那个位置的最小值

python 复制代码
class MinStack(object):

    def __init__(self):
        #minstack栈顶是维护和stack相同长度目前为止最小的元素
        self.stack=[]
        self.minstack=[]

    def push(self, val):
        """
        :type val: int
        :rtype: None
        """
        if not self.minstack:
            self.minstack.append(val)
        else:
            self.minstack.append(min(self.minstack[-1],val))
        self.stack.append(val)
    def pop(self):
        """
        :rtype: None
        """
        self.stack.pop()
        self.minstack.pop()
        

    def top(self):
        """
        :rtype: int
        """

        return self.stack[-1]

    def getMin(self):
        """
        :rtype: int
        """
        return self.minstack[-1]
        
相关推荐
yaoh.wang2 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
T1ssy2 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
醇氧2 小时前
【Windows】优雅启动:解析一个 Java 服务的后台启动脚本
java·开发语言·windows
hetao17338373 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
小鸡吃米…3 小时前
Python PyQt6教程七-控件
数据库·python
1916zz3 小时前
Extreme programing 方利喆 _ 江贤晟
python
长安牧笛3 小时前
智能鞋柜—脚气终结者,内置温湿度传感器和紫外线灯,晚上回家,把鞋放进去,自动检测湿度,湿度超标就启动烘干+紫外线杀菌,第二天穿鞋干燥无异味。
python
鲨莎分不晴3 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
weixin_457760003 小时前
PIL库将图片位深度是1、8、32统一转换为24的方法
python
搞科研的小刘选手4 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议