LeetCode 739. 每日温度

解题思路

单调栈的经典题型。

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

相关代码

复制代码
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;
    }
}
相关推荐
All for pursuit.5 分钟前
【动态规划-6】96.不同的二叉搜索树
数据结构·c++·算法·leetcode·动态规划
rou15 分钟前
Attention(注意力机制)
算法
信奥卷王35 分钟前
[GESP202609 六级] 数组划分
数据结构·算法
牧羊人.3331 小时前
动手学深度学习 07|Resnet网络与迁移学习
网络·人工智能·深度学习·神经网络·算法·cnn·迁移学习
All for pursuit.2 小时前
【并查集-2】399.除法求值
数据结构·c++·算法·leetcode
迪丽热爱2 小时前
一、C语言概述与程序结构
c语言·开发语言·算法
码爸2 小时前
动态规划介绍
算法·动态规划
怪奇云呼军3 小时前
教育试听预约后没到课,闪电智能 Voice Agent 如何做分层回访?
人工智能·python·算法·云计算·音视频
Nil2083 小时前
leetcode 416分割等和子集
算法·leetcode·职场和发展
云淡风轻~窗明几净3 小时前
角谷猜想的进展之三 移动了群山/沸騰的群山 2026-10-10
人工智能·算法·dubbo·图论