【LeetCode】739. 每日温度

维护一个存储下标的单调栈

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> stack;
int i=0;
int index=-1;
int n=temperatures.size();
vector<int> ans(n,0);
while(i<n){
while(index >= 0 &&temperatures[i]>temperatures[stack[index]]){
ans[stack[index]]=i-stack[index];
stack.pop_back();
index--;

}
stack.push_back(i);
index++;
i++;
}

return ans;
}
};
  • Copyrights © 2019-2024 Hxy
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信