leetcode#棧#739. 每日溫度

請根據每日 氣溫 列表,重新生成一個列表。對應位置的輸出爲:要想觀測到更高的氣溫,至少需要等待的天數。如果氣溫在這之後都不會升高,請在該位置用 0 來代替。

例如,給定一個列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的輸出應該是 [1, 1, 4, 2, 1, 1, 0, 0]。

提示:氣溫 列表長度的範圍是 [1, 30000]。每個氣溫的值的均爲華氏度,都是在 [30, 100] 範圍內的整數。

class Solution {
    public int[] dailyTemperatures(int[] T) {
        Deque<Integer> s = new LinkedList<>();
        int[] res = new int[T.length];
        for(int i=0;i<T.length;++i) {
            int temp = T[i];
            while(s.isEmpty()==false && T[s.peek()]<temp) {
                int pre = s.pop();
                res[pre] = i-pre;
            }
            s.push(i);
        }
        return res;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章