class Solution { public: vector<int> dailyTemperatures(vector<int>& temperatures) { int n=temperatures.size(); vector<int> res(n,0); stack<int> s; //从后往前 单调栈写法 for(int i=n-1;i>=0;i--) { while(!s.empty() && temperatures[i]>=temperatures[s.top()]) { s.pop(); } if(!s.empty()) { res[i]=s.top()-i; } s.push(i); } //从前往后单调栈写法 /* for(int i=0;i<n;i++) { while(!s.empty() && temperatures[i]>temperatures[s.top()]) { res[s.top()]=i-s.top(); s.pop(); } s.push(i); } */ return res; } };
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back