739每日温度 Posted on 2020-05-04 Words count in article: 234 | Reading time ≈ 1 739每日温度 下面是给出的代码123456class Solution {public: vector<int> dailyTemperatures(vector<int>& T) { }}; 我拿到必用BF123456789101112131415161718192021class Solution {public: vector<int> dailyTemperatures(vector<int>& T) { int n = T.size(); vector<int>v(n,0); stack<int>st; for(int i = 0;i<n;i++) { //每次拿到一个i,往后遍历找到比它大的值,然后记录 for(int j = i;j<n;j++) { if(T[j]>T[i]) { v[i] = j-i; break; } } } return v; }}; 但是很可惜,超出了时间限制 所以用递减栈来试试1234567891011121314151617181920class Solution {public: vector<int> dailyTemperatures(vector<int>& T) { int n = T.size(); vector<int>v(n,0); stack<int>s; for(int i= 0;i<n;i++) { //栈内存储的是序号,做差获得答案 while(!s.empty()&&T[i]>T[s.top()]) { int temp = s.top(); s.pop(); v[temp] = i-temp; } s.push(i); } return v; }}; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/05/04/739%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.