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;
    }
}
相关推荐
Forget the Dream25 分钟前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
Pniubi2 小时前
力扣21合并两个有序链表
算法·leetcode·职场和发展
黑十2 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能
AIgorithmGEEK2 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
神明不懂浪漫2 小时前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
用户54817845909503 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
文丑颜不良啊3 小时前
LeetCode3341~3360题目解析
java·leetcode
hai_android3 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
用户9338104348453 小时前
MyBatis-Plus LambdaQueryWrapper 隐式转换踩坑:不知不觉索引失效
java
用户3721574261353 小时前
Java 在 PDF 中查找关键词:获取页码和坐标,并高亮匹配结果
java