力扣739.每日温度
-
单调栈
- 从右到左做
- 栈中存下标
-
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; } };