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;
    }
}
相关推荐
a伊雪17 分钟前
c++ 引用参数
c++·算法
Data_agent1 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2401_871260021 小时前
Java学习笔记(二)面向对象
java·python·学习
是梦终空1 小时前
计算机毕业设计252—基于Java+Springboot+vue3+协同过滤推荐算法的农产品销售系统(源代码+数据库+2万字论文)
java·spring boot·vue·毕业设计·源代码·协同过滤算法·农产品销售系统
2301_764441332 小时前
使用python构建的应急物资代储博弈模型
开发语言·python·算法
丿BAIKAL巛2 小时前
Java前后端传参与接收全解析
java·开发语言
小熳芋2 小时前
验证二叉搜索树- python-递归&上下界约束
数据结构
cc蒲公英2 小时前
javascript有哪些内置对象
java·前端·javascript
guslegend2 小时前
Spring AOP高级应用与源码剖析
java
Rover.x2 小时前
head table is mandatory
java·apache