題目描述:
請(qǐng)根據(jù)每日 氣溫 列表,重新生成一個(gè)列表。對(duì)應(yīng)位置的輸出為:要想觀測到更高的氣溫,至少需要等待的天數(shù)。如果氣溫在這之后都不會(huì)升高,請(qǐng)?jiān)谠撐恢糜?0 來代替。
例如,給定一個(gè)列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的輸出應(yīng)該是 [1, 1, 4, 2, 1, 1, 0, 0]。
提示:氣溫 列表長度的范圍是 [1, 30000]。每個(gè)氣溫的值的均為華氏度,都是在 [30, 100] 范圍內(nèi)的整數(shù)。
作者:MisterBooo
鏈接:https://leetcode-cn.com/problems/daily-temperatures/solution/leetcode-tu-jie-739mei-ri-wen-du-by-misterbooo/
來源:力扣(LeetCode)
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
Java代碼:
class Solution {
public int[] dailyTemperatures(int[] T) {
int len = T.length;
int[] res = new int[len];
Stack<Integer> stack = new Stack<>();
for(int i = 0;i < len;i++) {
while(!stack.isEmpty() && T[i] > T[stack.peek()]) {
int pre = stack.pop();
res[pre] = i - pre;
}
stack.add(i);
}
return res;
}
}