LeetCode 739. 每日温度

解题思路

单调栈的经典题型。

我们需要找到temperatures[i]的右边最近的小于temperatures[i]的值,所以就想到了单调栈。

相关代码

复制代码
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();
        int res[] = new int[temperatures.length];
        int n = temperatures.length;
        for(int i=n-1;i>=0;i--){
            //进行筛选
            while(stack.isEmpty()==false&&temperatures[i]>=temperatures[stack.peek()]) stack.pop();
            if(stack.isEmpty()==true) res[i]=0;
            else res[i]=Math.abs(stack.peek()-i);
            stack.push(i);
        }
        return res;
    }
}
相关推荐
WindSearcher1 小时前
大模型微调相关知识
后端·算法
取酒鱼食--【余九】1 小时前
rl_sar实现sim2real的整体思路
人工智能·笔记·算法·rl_sar
Magnum Lehar3 小时前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
水蓝烟雨3 小时前
[面试精选] 0094. 二叉树的中序遍历
算法·面试精选
超闻逸事3 小时前
【题解】[UTPC2024] C.Card Deck
c++·算法
暴力求解3 小时前
C++类和对象(上)
开发语言·c++·算法
JKHaaa4 小时前
几种简单的排序算法(C语言)
c语言·算法·排序算法
让我们一起加油好吗4 小时前
【基础算法】枚举(普通枚举、二进制枚举)
开发语言·c++·算法·二进制·枚举·位运算
FogLetter4 小时前
微信红包算法揭秘:从随机性到产品思维的完美结合
算法
BUG收容所所长4 小时前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法