Skip to main content

🟡 剑指 Offer II 038. 每日温度

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

class Solution {    public int[] dailyTemperatures(int[] temperatures) {        // [idx, temp]        Deque<int[]> stack = new LinkedList<>();        int n = temperatures.length, cur;        int[] res = new int[n];
        for (int i=n-1; i>=0; i-=1) {            cur = temperatures[i];
            if (stack.isEmpty()) {                res[i] = 0;                stack.add(new int[]{i, cur});                continue;            }
            while (!stack.isEmpty() && stack.getLast()[1] <= cur) {                stack.removeLast();            }
            if (stack.isEmpty()) {                res[i] = 0;            } else {                res[i] = stack.getLast()[0] - i;            }
            stack.add(new int[]{i, cur});        }
        return res;    }}