1. 程式人生 > 其它 >[Leetcode] 力扣739:每日溫度

[Leetcode] 力扣739:每日溫度

技術標籤:Leetcodeleetcodestack

在這裡插入圖片描述
思路
“下一個更大”問題,使用單調棧進行解答

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; } };