力扣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;
      }
  };
相关推荐
UIU1144 分钟前
P1003 [NOIP 2011 提高组] 铺地毯
数据结构·c++·算法
爱读书的小胖8 分钟前
LeetCode 88. 合并两个有序数组 C 解题分析
c语言·数据结构·算法
员工风险治理专家黄海波1 小时前
商业利益冲突预防性内控与职责分离如何减少主观偏差
职场和发展
垆边人似月.1 小时前
删边使每个连通块权值和为 target
算法·深度优先·图论
xxwl5851 小时前
区域赛算法模版第四章---数据结构
数据结构·c++·算法
xxwl5852 小时前
第六章---计算几何
笔记·学习·算法
Logic1013 小时前
C语言/数据结构二维数组题解:找出行列O个数均为偶数的格子——奇偶性预处理+二次遍历
c语言·数据结构·算法·二维数组·时间复杂度·动态内存分配·奇偶性
程序猿阿森3 小时前
【机器学习实战】朴素贝叶斯算法详解:从概率基础到商品评论情感分析
算法·机器学习·概率论
xxwl5853 小时前
区域赛算法模版第五章---数学与数论
c++·笔记·算法