Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Constraints:
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/daily-temperatures
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
我的解答:
// 单调栈法, 栈里只能存比栈顶更小的,那相等呢
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int len = temperatures.size();
// 存数组temperatures的索引
stack<int> myStack;
vector<int> answers(len, 0); // 最后一个,一定是0
int top;
for(int i = 0; i < len; i ++){ //不是i<len,因为最后一个元素会影响前边的结果
if( myStack.empty() ) {
myStack.push(i);
continue;
}else{
// if(!myStack.empty())
top = myStack.top();
if(temperatures[i] > temperatures[top]){
while(!myStack.empty() && temperatures[i] > temperatures[top]){
answers[top] = i - top;
myStack.pop();
if(!myStack.empty()) top = myStack.top();
}
myStack.push(i);
}else{ // 后边温度更小,?或者相等
myStack.push(i);
}
}
}
return answers;
}
};