力扣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;
      }
  };
相关推荐
秋天的怀念_David4 小时前
算法一千零一夜 · 作品相关:版权与致谢声明
程序人生·算法·学习方法
揽星河..4 小时前
【C语言】数组
c语言·开发语言·算法
careathers4 小时前
【数据结构】基于比较的排序算法 - 选择排序
数据结构·算法·排序算法
听风若依4 小时前
C++模板初阶
c++·算法
纪念 2295 小时前
C++ 算法(模拟)
开发语言·c++·算法
揽星河..5 小时前
【C语言】函数
c语言·算法
zwc32104320555 小时前
Day 2:图像内存、数据类型与stride
c++·人工智能·opencv·算法·计算机视觉
重生之小比特5 小时前
【算法拾光】模拟|提莫攻击 + N 字形变换
算法·模拟
東隅已逝,桑榆非晚6 小时前
AVL数实现
c++·笔记·学习·算法
驭渊的小故事6 小时前
LeetCode 算法题解:滑动窗口与数组合并的实战应用
算法·leetcode·职场和发展