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;
    }
}
相关推荐
代码游侠10 小时前
学习笔记——线程控制 - 互斥与同步
linux·运维·笔记·学习·算法
yaoh.wang10 小时前
力扣(LeetCode) 66: 加一 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
wanderist.11 小时前
2025年蓝桥杯省赛C++大学A组
c++·算法·蓝桥杯
啊董dong11 小时前
noi-2025年12月16号作业
数据结构·c++·算法·noi
white-persist11 小时前
【攻防世界】reverse | simple-check-100 详细题解 WP
c语言·开发语言·汇编·数据结构·c++·python·算法
长安er11 小时前
LeetCode 01 背包 & 完全背包 题型总结
数据结构·算法·leetcode·动态规划·背包问题
小南家的青蛙11 小时前
LeetCode第2658题 - 网格图中鱼的最大数目
算法·leetcode·职场和发展
ZHang......11 小时前
LeetCode 1114. 按序打印
java·开发语言·算法
仰泳的熊猫12 小时前
1083 List Grades
数据结构·c++·算法·pat考试
Tan_Zhixia12 小时前
时间复杂度判断
数据结构·c++·算法