分析
- 题目:739. 每日温度
- 思路:右下降栈,模板题。
- 考查点: 单调栈
代码
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& A) {
// 翻译一下:找到下一个更大更大元素 右下降栈
int n = A.size();
vector<int> res(n);
stack<int> stk;
for(int i = n-1; i >= 0; i--){
while(stk.size() && A[stk.top()] <= A[i]) stk.pop();
if(stk.empty()) res[i] = 0;
else res[i] = stk.top() - i;
stk.push(i);
}
return res;
}
};