(栈)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]
        
相关推荐
NAGNIP3 小时前
万字长文!回归模型最全讲解!
算法·面试
知乎的哥廷根数学学派3 小时前
面向可信机械故障诊断的自适应置信度惩罚深度校准算法(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习·矩阵
且去填词3 小时前
DeepSeek :基于 Schema 推理与自愈机制的智能 ETL
数据仓库·人工智能·python·语言模型·etl·schema·deepseek
人工干智能3 小时前
OpenAI Assistants API 中 client.beta.threads.messages.create方法,兼谈一星*和两星**解包
python·llm
databook4 小时前
当条形图遇上极坐标:径向与圆形条形图的视觉革命
python·数据分析·数据可视化
猫头虎4 小时前
如何在浏览器里体验 Windows在线模拟器:2026最新在线windows模拟器资源合集与技术揭秘
运维·网络·windows·系统架构·开源·运维开发·开源软件
阿部多瑞 ABU4 小时前
`chenmo` —— 可编程元叙事引擎 V2.3+
linux·人工智能·python·ai写作
acanab4 小时前
VScode python插件
ide·vscode·python
666HZ6664 小时前
数据结构2.0 线性表
c语言·数据结构·算法
知乎的哥廷根数学学派5 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习