力扣:739. 每日温度

739. 每日温度

典型的单调栈问题,从右边遍历枚举维护单调栈即可。

答案存到数组ant中,

当前位置温度比栈顶大就栈顶--,退出循环再栈顶加入当前温度。

cpp 复制代码
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        
       const int N=temperatures.size();
        int stk[N+2];int tt=0;
        vector<int> ant(N);
        for(int i=N-1;i>=0;i--)
        {
            while(tt && temperatures[i]>=temperatures[stk[tt]])tt--;
            stk[++tt]=i;
            if(tt>1) ant[i]=stk[tt-1]-i;
            
        }
        return ant;
    }
};
相关推荐
hh随便起个名5 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
橘子真甜~5 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
小浣熊熊熊熊熊熊熊丶6 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子6 小时前
JDK 安装配置
java·开发语言
星哥说事6 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....6 小时前
Miniconda使用
开发语言·python
zfj3216 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧6 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop6 小时前
Aes加密 GCM java
java·开发语言·python
weixin_462446237 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang