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;
    }
}
相关推荐
电科_银尘21 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
星落梦生1 天前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵1 天前
带您看懂字节码指令(四):一条指令里有什么
java·jvm
最强小杰1 天前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
Flynn Liang1 天前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
学长毕设1 天前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
曹牧1 天前
Java:“刷新雪崩”
java·开发语言
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David1 天前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Forget the Dream1 天前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai