力扣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;
      }
  };
相关推荐
Coder-magician12 分钟前
图论总结篇
算法·图论
有同事要进步18 分钟前
Leecode第68题:x的平方根
java·数据结构·算法
吹什么轩25 分钟前
linux系统复习:进程优先级与O(1)调度
java·linux·算法
开开心心就好35 分钟前
安卓PDF处理工具,免费无广告零弹窗
android·服务器·开发语言·算法·pdf·ocr
zyl76516 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
爱读书的小胖20 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌20 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
ychqsq21 小时前
234.无题
经验分享·职场和发展
OPEN-F21 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_3077791321 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab