力扣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;
      }
  };
相关推荐
ysu_03147 小时前
05 | 持久化撤销提示非核心功能
算法·游戏程序
浮沉9878 小时前
二分查找算法概述&通用模板
算法
Keven_119 小时前
算法札记:SPFA判负环算法的证明
算法
什巳9 小时前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
Jerry10 小时前
LeetCode 347. 前 K 个高频元素
算法
Young Doro10 小时前
SAC 算法
线性代数·算法·机器学习
罗超驿10 小时前
2.算法效率的核心密码:时间复杂度和空间复杂度详解
java·数据结构·算法
:-)11 小时前
算法-堆排序
数据结构·算法·排序算法