【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;
    }
};
相关推荐
u***j3242 分钟前
算法设计模式总结
算法·设计模式
vir0212 分钟前
交换瓶子(贪心)
数据结构·算法
G***66911 小时前
算法设计模式:贪心与动态规划
算法·设计模式·动态规划
墨染点香1 小时前
LeetCode 刷题【160. 相交链表】
算法·leetcode·链表
少睡点觉1 小时前
LeetCode 238. 除自身以外数组的乘积 问题分析+解析
java·算法·leetcode
YoungHong19921 小时前
面试经典150题[066]:分隔链表(LeetCode 86)
leetcode·链表·面试
大千AI助手1 小时前
多叉树:核心概念、算法实现与全领域应用
人工智能·算法·决策树·机器学习··多叉树·大千ai助手
一只老丸2 小时前
HOT100题打卡第38天——贪心算法
算法·贪心算法
普通网友2 小时前
高性能TCP服务器设计
开发语言·c++·算法
醒过来摸鱼2 小时前
9.12 sinc插值
python·线性代数·算法·numpy