【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;
    }
};
相关推荐
稚辉君.MCA_P8_Java1 小时前
Gemini永久会员 Java实现的暴力递归版本
java·数据结构·算法
冯诺依曼的锦鲤2 小时前
算法练习:差分
c++·学习·算法
有意义2 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
鹿鹿鹿鹿isNotDefined2 小时前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
封奚泽优2 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER3 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
高洁013 小时前
【无标具身智能-多任务与元学习】
神经网络·算法·aigc·transformer·知识图谱
leoufung3 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
识醉沉香3 小时前
广度优先遍历
算法·宽度优先
中國龍在廣州3 小时前
现在人工智能的研究路径可能走反了
人工智能·算法·搜索引擎·chatgpt·机器人