(栈)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]
        
相关推荐
RabbitYao9 分钟前
Android 项目 通过 AndroidStringsTool 更新多语言词条
android·python
RabbitYao12 分钟前
使用 Gemini 及 Python 更新 Android 多语言 Excel 文件
android·python
Davis_121914 分钟前
代码随想录算法训练营27天 | 56. 合并区间、738.单调递增的数字、968.监控二叉树(提高)
数据结构·c++·算法·leetcode·贪心算法
闻缺陷则喜何志丹16 分钟前
【倍增 桶排序】后缀数组
c++·算法·倍增·桶排序·后缀数组·lcp·后缀树
天天进步201517 分钟前
Python机器学习入门:用scikit-learn构建你的第一个预测模型
python·机器学习·scikit-learn
程序员岳焱27 分钟前
使用 JPype 实现 Java 与 Python 的深度交互
java·后端·python
站大爷IP29 分钟前
Python处理JSON数据的最佳实践:从基础到进阶的实用指南
python
amazinging1 小时前
北京-15k测试-入职甲方金融-上班第二天
python·金融
站大爷IP1 小时前
Django中间件自定义开发指南:从原理到实战的深度解析
python
跟橙姐学代码1 小时前
Python 高手都偷偷用的 Lambda 函数,你还在傻傻写 def 吗?
前端·python