LeetCode 739. 每日温度

OJ链接: 739. 每日温度

示例代码:

复制代码
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
            int length =  temperatures.length;
    //数组存储下标记录
            int[] ans = new int[length];
            Stack<Integer> stack = new Stack<>();

            for(int i =0 ; i<length ;i++){
    //遍历每个温度
                int temp = temperatures[i]; 
           //当栈不为空 且 当前温度大于栈顶温度
                while( !stack.empty() && temp > temperatures[stack.peek()]){
    //出栈 ,并记录下标
                   int perv = stack.pop();
                    ans[perv] = i - perv;
                }
                stack.push(i);
            }
            return ans;
    }
}
相关推荐
lUie INGA44 分钟前
在2023idea中如何创建SpringBoot
java·spring boot·后端
geBR OTTE1 小时前
SpringBoot中整合ONLYOFFICE在线编辑
java·spring boot·后端
Porunarufu1 小时前
博客系统UI自动化测试报告
java
不爱吃炸鸡柳2 小时前
数据结构精讲:树 → 二叉树 → 堆 从入门到实战
开发语言·数据结构
Aurorar0rua2 小时前
CS50 x 2024 Notes C - 05
java·c语言·数据结构
Cosmoshhhyyy3 小时前
《Effective Java》解读第49条:检查参数的有效性
java·开发语言
布谷歌3 小时前
常见的OOM错误 ( OutOfMemoryError全类型详解)
java·开发语言
6Hzlia3 小时前
【Hot 100 刷题计划】 LeetCode 739. 每日温度 | C++ 逆序单调栈
c++·算法·leetcode
eLIN TECE3 小时前
springboot和springframework版本依赖关系
java·spring boot·后端