请根据每日 气温
列表 temperatures
,重新生成一个列表,要求其对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 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]
提示:
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
if (temperatures == null) {
return null;
}
int[] result = new int[temperatures.length];
Stack<Pair<Integer, Integer>> myStack = new Stack<>();
for (int i = 0; i < temperatures.length; i++) {
while (!myStack.isEmpty() && myStack.peek().getValue() < temperatures[i]) {
Pair<Integer, Integer> myPair = myStack.pop();
result[myPair.getKey()] = i - myPair.getKey();
}
myStack.push(new Pair<>(i, temperatures[i]));
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)