# 739. 每日温度
给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
示例 1:
```
输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]
```
示例 2:
```
输入: temperatures = [30,40,50,60]
输出: [1,1,1,0]
```
示例 3:
```
输入: temperatures = [30,60,90]
输出: [1,1,0]
```
題解思路:
遍歷陣列時把每個數字的下標存入棧中,當當前數組遇到到比棧頂來的大時,說明棧頂數組下一個比它大的數是當前數,把前面數組的下標移出棧,並把當前數組和棧頂的數組下標之間的差存入ans裡。
在確定棧頂的數後,把下標存入棧中。
```java=
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] ans = new int[n];
Stack<Integer> indexes = new Stack<>();
for(int curIndex = 0;curIndex<n;curIndex++){
while(!indexes.isEmpty() && temperatures[curIndex] > temperatures[indexes.peek()]){
int preIndex = indexes.pop();
ans[preIndex] = curIndex -preIndex;
}
indexes.add(curIndex);
}
return ans;
}
}
```