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.82442 分钟前
Agent memory发展路线
算法
青桔柠薯片1 小时前
Linux I/O多路复用:深入浅出poll与epoll
linux·运维·服务器·算法
哈哈很哈哈1 小时前
逻辑回归Logistic Regression
算法·机器学习·逻辑回归
甄心爱学习2 小时前
【极大似然估计/最大化后验】为什么逻辑回归要使用交叉熵损失函数
算法·机器学习·逻辑回归
郝学胜-神的一滴2 小时前
深度学习入门全解析:从核心概念到实战基础 | 技术研讨会精华总结
人工智能·python·深度学习·算法·cnn
梯度下降中2 小时前
CNN原理精讲
人工智能·算法·机器学习
Ivanqhz2 小时前
活跃范围重写(Live Range Rewriting)
开发语言·c++·后端·算法·rust
xiaoye-duck2 小时前
《算法题讲解指南:优选算法-链表》--51.两数相加,52.两两交换链表中的节点
数据结构·算法·链表
Cosolar2 小时前
阿里CoPaw进阶使用手册:从新手到高手的完整指南
人工智能·后端·算法
松小白song2 小时前
机器人路径规划算法之Dijkstra算法详解+MATLAB代码实现
前端·javascript·算法