(栈)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]
        
相关推荐
元亓亓亓20 小时前
LeetCode热题100--79. 单词搜索
算法·leetcode·职场和发展
用户23452670098220 小时前
Python实现异步任务队列深度好文
后端·python
夫唯不争,故无尤也20 小时前
PyTorch 的维度变形一站式入门
人工智能·pytorch·python
司铭鸿20 小时前
化学式解析的算法之美:从原子计数到栈的巧妙运用
linux·运维·服务器·算法·动态规划·代理模式·哈希算法
熊猫钓鱼>_>20 小时前
从零开始构建RPG游戏战斗系统:实战心得与技术要点
开发语言·人工智能·经验分享·python·游戏·ai·qoder
2501_9411437321 小时前
缓存中间件Redis与Memcached在高并发互联网系统优化与实践经验分享
leetcode
BoBoZz1921 小时前
TriangleStrip连续三角带
python·vtk·图形渲染·图形处理
ekprada21 小时前
DAY 18 推断聚类后簇的类型
算法·机器学习·支持向量机
生信大表哥21 小时前
Python单细胞分析-基于leiden算法的降维聚类
linux·python·算法·生信·数信院生信服务器·生信云服务器
一晌小贪欢21 小时前
【Python办公】用 Selenium 自动化网页批量录入
开发语言·python·selenium·自动化·python3·python学习·网页自动化