單調棧 力扣 739. 每日溫度

在這裏插入圖片描述

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        vector<int> res(T.size(),0);
        T.push_back(INT_MIN);

        stack<int> st;
        for(int i = 0;i < T.size();i++){
            if(st.empty() || T[i] < T[st.top()]){
                st.push(i);
            }
            while(!st.empty() && T[i] > T[st.top()]){
                res[st.top()] = i - st.top();
                 st.pop();
            }
            st.push(i);
        }
        return res;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章