LeetCode739. 每日温度(单调栈)

739. 每日温度

class Solution {
public:
    //单调栈:时间O(n), 空间O(n)
    vector<int> dailyTemperatures(vector<int>& T) {
        int len = T.size();
        vector<int> res(len, 0);
        stack<int> st;
        for(int i = 0; i < len; i++) {
            while(!st.empty() && T[i] > T[st.top()]) {
                int top = st.top();
                st.pop();
                res[top] = i - top; 
            }
            st.push(i);
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode刷题)