解题思路有两点比较关键:
1、想到用栈,而且栈中存放的是索引,而不是温度值
2、打破思维桎梏,不是按顺序来计算D中每一个值的
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
vector<int> D(T.size(), 0);
stack<int> temp;
temp.push(0);
int index;
for(int i=1; i<T.size(); i++) {
while(!temp.empty() && T[temp.top()] < T[i]) {
D[temp.top()] = i - temp.top();
temp.pop();
}
temp.push(i);
}
return D;
}
};