剑指 Offer II 038. 每日温度 (单调递减栈)

剑指 Offer II 038. 每日温度

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
		stack<int> S;
		int size=temperatures.size();
		vector<int>res(size,0);
		for(int i=0;i<size;++i){
			while(!S.empty()&&temperatures[i]>temperatures[S.top()]){
				int preIndex=S.top();
				res[preIndex]=i-preIndex;
				S.pop();
			}
			S.push(i);	
		}
		return res;
    }
};

你可能感兴趣的:(剑指offer,leetcode,算法)