【LeetCode】739. 每日温度

题目

739. 每日温度

思路

创建一个栈,遍历输入,如果栈为空则直接压入,如果栈非空且当前温度大于栈顶元素,则弹出栈顶元素,并且a[pre]=i-pre,pre为栈顶元素,如果当前温度小于栈顶元素,则直接压入栈中。

代码

cpp 复制代码
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n=temperatures.size();
        vector<int>a(n);
        stack<int>v;
        for(int i=0;i<n;i++)
        {
            while(!v.empty() && temperatures[i]>temperatures[v.top()])
            {
                int pre=v.top();
                a[pre]=i-pre;
                v.pop();
            }
            v.push(i);
        }
            return a;
    }
};
相关推荐
gfdhy6 分钟前
【Linux】服务器网络与安全核心配置|静态IP+SSH加固+防火墙,公网服务器必学实操
linux·服务器·网络·tcp/ip·算法·安全·哈希算法
Frostnova丶15 分钟前
LeetCode 1888 使二进制字符串交替的最少翻转次数
算法·leetcode
王码码203519 分钟前
Flutter for OpenHarmony:es_compression — 高性能 Brotli 与 Zstd 算法实战
算法·flutter·elasticsearch
苏纪云1 小时前
蓝桥杯知识点——day2
数据结构·算法·蓝桥杯
Wect1 小时前
LeetCode 52. N 皇后 II:回溯算法高效求解
前端·算法·typescript
iFlyCai1 小时前
数据结构与算法之希尔排序
数据结构·算法·排序算法
Nontee1 小时前
Leetcode Top100答案和解释 -- Python版本(矩阵)
python·leetcode·矩阵
lcreek1 小时前
LeetCode2208. 将数组和减半的最少操作次数、LeetCode2406.将区间分为最少组数
python·算法
shehuiyuelaiyuehao1 小时前
算法1,移动零
数据结构·算法·排序算法
shehuiyuelaiyuehao1 小时前
算法2,复写零
数据结构·算法