(栈)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]
        
相关推荐
小W与影刀RPA4 分钟前
【影刀 RPA】 :文档敏感词批量替换,省时省力又高效
人工智能·python·低代码·自动化·rpa·影刀rpa
weixin_395448915 分钟前
main.c_cursor_0202
前端·网络·算法
Python+JAVA+大数据10 分钟前
TCP_IP协议栈深度解析
java·网络·python·网络协议·tcp/ip·计算机网络·三次握手
senijusene11 分钟前
数据结构与算法:队列与树形结构详细总结
开发语言·数据结构·算法
杜家老五11 分钟前
综合实力与专业服务深度解析 2026北京网站制作公司六大优选
数据结构·算法·线性回归·启发式算法·模拟退火算法
一个无名的炼丹师25 分钟前
多模态RAG系统进阶:从零掌握olmOCR与MinerU的部署与应用
python·大模型·ocr·多模态·rag
u01092727140 分钟前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
2301_7657031443 分钟前
C++与自动驾驶系统
开发语言·c++·算法
Ll13045252981 小时前
Leetcode二叉树 part1
b树·算法·leetcode
MediaTea1 小时前
<span class=“js_title_inner“>Python:实例对象</span>
开发语言·前端·javascript·python·ecmascript