[Leetcode] 力扣739:每日温度

在这里插入图片描述
思路
“下一个更大”问题,使用单调栈进行解答

class Solution {
    
    
public:
    vector<int> dailyTemperatures(vector<int>& T) {
    
    
        stack<int> s;
        vector<int> res(T.size());
        for (int i = 0; i< T.size(); i++) {
    
    
            while (!s.empty() && T[i] > T[s.top()]) {
    
    
                int former = s.top();
                res[former] = i - former;
                s.pop();
            }
            s.push(i);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_44537258/article/details/112546648