力扣: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;
    }
};
相关推荐
ᅠᅠᅠ@几秒前
异常枚举;
开发语言·javascript·ecmascript
凌肖战6 分钟前
力扣上刷题之C语言实现(数组)
c语言·算法·leetcode
编程版小新6 分钟前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx26 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
秋夫人33 分钟前
B+树(B+TREE)索引
数据结构·算法
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构