739. 每日温度

739. 每日温度


题目链接:739. 每日温度

代码如下:

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) 
    {
        vector<int> res(temperatures.size(),0);
        stack<int> sta;//递增栈
        sta.push(0);
        for(int i=1;i<temperatures.size();i++)
        {
            if(temperatures[i]<temperatures[sta.top()])//当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况
                sta.push(i);
            else if(temperatures[i]==temperatures[sta.top()])//当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况
                sta.push(i);
            else//当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况
            {
                while(!sta.empty()&&temperatures[i]>temperatures[sta.top()])
                {
                    res[sta.top()]=i-sta.top();
                    sta.pop();
                }
                sta.push(i);
            }
        }
        return res;
    }
};

相关推荐

  1. 【LeetCode739每日温度

    2024-03-14 03:02:02       61 阅读
  2. Leetcode 739. 每日温度

    2024-03-14 03:02:02       53 阅读
  3. 739. 每日温度

    2024-03-14 03:02:02       45 阅读
  4. 代码随想录 739. 每日温度

    2024-03-14 03:02:02       59 阅读
  5. LeetCode-739. 每日温度【栈 数组 单调栈】

    2024-03-14 03:02:02       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 03:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 03:02:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 03:02:02       82 阅读
  4. Python语言-面向对象

    2024-03-14 03:02:02       91 阅读

热门阅读

  1. OpenCV-绘制图形

    2024-03-14 03:02:02       40 阅读
  2. C++容器——unordered_map浅谈

    2024-03-14 03:02:02       49 阅读
  3. Keras用tf的Strategy()分布式训练时候报XLA错误

    2024-03-14 03:02:02       52 阅读
  4. 鸿蒙跨包跳转页面-HSP页面路由

    2024-03-14 03:02:02       52 阅读
  5. 四川大学校园网自动登录

    2024-03-14 03:02:02       43 阅读