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;
    }
}
相关推荐
冯诺依曼的锦鲤20 小时前
算法练习:差分
c++·学习·算法
许商20 小时前
【stm32】【printf】
java·前端·stm32
JIngJaneIL20 小时前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·论文·智慧物业管理系统
ANYOLY20 小时前
Redis 面试题库
java·redis·面试
有意义20 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
懒惰蜗牛20 小时前
Day63 | Java IO之NIO三件套--选择器(下)
java·nio·选择器·selector·半包粘包·tcp缓冲区
鹿鹿鹿鹿isNotDefined20 小时前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
JavaGuide20 小时前
美团2026届后端一二面(附详细参考答案)
java·后端
打工人你好20 小时前
如何设计更安全的 VIP 权限体系
java·jvm·安全
L.EscaRC20 小时前
Spring IOC核心原理与运用
java·spring·ioc