力扣739.每日温度

力扣739.每日温度

  • 单调栈

    • 从右到左做
    • 栈中存下标
cpp 复制代码
  class Solution {
  public:
      vector<int> dailyTemperatures(vector<int>& temperatures) {
          int n = temperatures.size();
          vector<int> ans(n);
          stack<int> st;
          for(int i=n-1;i>=0;i--)
          {
              int t = temperatures[i];
              //说明他不会是任何数的右边界
              while(!st.empty() && t >= temperatures[st.top()])
                  st.pop();
              if(!st.empty())
                  ans[i] = st.top() - i;
              st.push(i);
          }
          return ans;
      }
  };
相关推荐
不会就选b13 小时前
算法日常・每日刷题--<哈希>1
算法·哈希算法
zander25813 小时前
LeetCode 207. 课程表
算法·深度优先
萌动的小火苗13 小时前
深度学习中的损失函数与优化算法基础知识
人工智能·深度学习·算法
happyprince13 小时前
02_OpenCodeReview 具体观:八种算法机制与背后的论文谱系
算法
变量未定义~13 小时前
图论+动态规划——魔法阵
算法
大爱编程♡13 小时前
C++基础-类和对象
c++·算法
ysa05103013 小时前
【板子】费用流
算法·深度优先·图论
玖玥拾13 小时前
LeetCode 80 删除有序数组中的重复项 II
算法·leetcode
青山木13 小时前
Hot 100 --- 电话号码的字母组合
java·数据结构·算法·leetcode·逻辑回归
hanlin0314 小时前
刷题笔记:力扣第206题-反转链表
笔记·leetcode·链表