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;
    }
}
相关推荐
一个响当当的名号40 分钟前
lectrue9 索引并发控制
java·开发语言·数据库
2401_8321319540 分钟前
模板错误消息优化
开发语言·c++·算法
金枪不摆鳍41 分钟前
算法--二叉搜索树
数据结构·c++·算法
进阶小白猿44 分钟前
Java技术八股学习Day30
java·开发语言·学习
近津薪荼1 小时前
优选算法——双指针6(单调性)
c++·学习·算法
向哆哆1 小时前
画栈 · 跨端画师接稿平台:基于 Flutter × OpenHarmony 的整体设计与数据结构解析
数据结构·flutter·开源·鸿蒙·openharmony·开源鸿蒙
helloworldandy1 小时前
高性能图像处理库
开发语言·c++·算法
2401_836563181 小时前
C++中的枚举类高级用法
开发语言·c++·算法
bantinghy1 小时前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
hhy_smile1 小时前
Class in Python
java·前端·python