【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;
    }
};
相关推荐
MadPrinter19 分钟前
Python 异步爬虫实战:FindQC 商品数据爬取系统完整教程
爬虫·python·算法·自动化
郝学胜-神的一滴21 分钟前
Effective Modern C++ 条款36:如果有异步的必要请指定std::launch::async
开发语言·数据结构·c++·算法
小此方23 分钟前
Re:从零开始的 C++ STL篇(六)一篇文章彻底掌握C++stack&queue&deque&priority_queue
开发语言·数据结构·c++·算法·stl
0 0 032 分钟前
CCF-CSP 40-2 数字变换(transform)【C++】考点:预处理
开发语言·c++·算法
香芋Yu34 分钟前
【强化学习教程——01_强化学习基石】第06章_Q-Learning与SARSA
人工智能·算法·强化学习·rl·sarsa·q-learning
回敲代码的猴子38 分钟前
2月15日打卡
数据结构·算法
菜鸡儿齐1 小时前
leetcode-和为k的子数组
java·算法·leetcode
踩坑记录1 小时前
leetcode hot100 437. 路径总和 III medium 前缀和 DFS
leetcode
1 小时前
2.15最大效益,螺旋方阵,方块转化
算法
im_AMBER2 小时前
Leetcode 122 二叉树的最近公共祖先 | 二叉搜索树迭代器
学习·算法·leetcode·二叉树