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;
    }
}
相关推荐
我叫黑大帅2 小时前
拒绝加字段堆屎山!利用位掩码与流程编排优雅落地 上图下文 复合状态
后端·算法·面试
Cosolar2 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
罗西的思考2 小时前
【轻量化复刻大模型 / KIMI K3】K3 PostTrain 后训练交互式演示
人工智能·算法·机器学习
Navigator_Z4 小时前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c
c语言·算法·leetcode
电科_银尘5 小时前
【大模型算法与工程 39】PPO:策略比率、裁剪目标与训练稳定性
人工智能·算法·机器学习
shehuiyuelaiyuehao6 小时前
算法68,字符串相乘
算法
Huangjin007_6 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
驭渊的小故事8 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
htzyl2069 小时前
金额计算:为什么前端不该用浮点数存钱
前端·javascript·算法
电科_银尘9 小时前
【大模型算法与工程 32】全量微调与参数高效微调:如何选择
人工智能·算法·机器学习