739. 每日温度
根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。
例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。
来源:力扣(LeetCode)
#include<vector>
#include<iostream>
#include<stack>
using namespace std;
class Solution{
public:
vector<int> dailyTemperatures(vector<int>& T){
vector<int> ans(T.size(),0);
stack<int> res;
for(int j=T.size()-1;j>=0;j--){
while(!res.empty()&&T[j]>=T[res.top()]) res.pop();
if(res.empty()) {
ans[j]=0;
}
else{
ans[j]=res.top()-j;
}
res.push(j);
}
return ans;
}
};
int main() {
Solution s;
vector<int> T={73, 74, 75, 71, 69, 72, 76, 73};
vector <int> ans;
ans=s.dailyTemperatures(T);
for(int i=0;i<ans.size();i++) cout<<ans[i]<<'\t';
}
还没有评论,来说两句吧...