当前位置:网站首页>LeetCode 739. 每日温度

LeetCode 739. 每日温度

2022-07-06 06:02:00 Sasakihaise_

739. 每日温度

【单调栈】

class Solution {

    // 单调栈 10:18 42

    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new LinkedList();
        int[] t = temperatures;
        int n = t.length;
        int[] ans = new int[n];
        for (var i = n - 1; i >= 0; i--) {
            while (!stack.isEmpty() && t[stack.peek()] <= t[i]) {
                stack.pop();
            }
            if (stack.isEmpty()) {
                ans[i] = 0;       
            } else {
                ans[i] = stack.peek() - i;
            }
            stack.push(i);
        }
        return ans;
    }
}

 

 

原网站

版权声明
本文为[Sasakihaise_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Sasakihaise_/article/details/125615620