LeetCode 739. 每日温度

解题思路

单调栈的经典题型。

我们需要找到temperatures[i]的右边最近的小于temperatures[i]的值,所以就想到了单调栈。

相关代码

复制代码
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();
        int res[] = new int[temperatures.length];
        int n = temperatures.length;
        for(int i=n-1;i>=0;i--){
            //进行筛选
            while(stack.isEmpty()==false&&temperatures[i]>=temperatures[stack.peek()]) stack.pop();
            if(stack.isEmpty()==true) res[i]=0;
            else res[i]=Math.abs(stack.peek()-i);
            stack.push(i);
        }
        return res;
    }
}
相关推荐
mit6.82411 分钟前
博弈dp|凸包|math分类
算法
Shinom1ya_33 分钟前
算法 day 41
数据结构·算法·leetcode
hetao17338371 小时前
2025-10-30 ZYZOJ Star(斯达)模拟赛 hetao1733837的record
c++·算法
无敌最俊朗@1 小时前
C++ 值类别与移动语义详解(精简版)
java·数据结构·算法
lingran__2 小时前
算法沉淀第十一天(序列异或)
c++·算法
一匹电信狗2 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
寂静山林2 小时前
UVa 11853 Paintball
算法
Theodore_10223 小时前
深度学习(10)模型评估、训练与选择
人工智能·深度学习·算法·机器学习·计算机视觉
五条凪3 小时前
Verilog-Eval-v1基准测试集搭建指南
开发语言·人工智能·算法·语言模型
是店小二呀3 小时前
从“算法思维”到“算子思维”:我在昇腾AI开发中的认知跃迁
人工智能·算法