美文网首页
739. 每日温度

739. 每日温度

作者: justonemoretry | 来源:发表于2021-10-27 23:01 被阅读0次
image.png

解法

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        // 维护一个单调递减栈,栈里面存放下标,找到大于栈顶的再跳出计算
        int len = temperatures.length;
        int[] res = new int[len];
        Stack<Integer> s = new Stack<>();
        for (int i = 0; i < len; i++) {
           // 当前大于栈顶,说明是温度升高的一天,可以将之前小于的点都算出来
            while (!s.isEmpty() && temperatures[i] > temperatures[s.peek()]) {
                int index = s.pop();
                res[index] = i - index;                
            }
            s.add(i);
        }
        return res;
    }
}

相关文章

  • 739. 每日温度

    739. 每日温度[https://leetcode.cn/problems/daily-temperatures...

  • 739. 每日温度

    739. 每日温度[https://leetcode-cn.com/problems/daily-temperat...

  • 739. 每日温度

    739. 每日温度[https://leetcode-cn.com/problems/daily-temperat...

  • 题739

    739. 每日温度[https://leetcode-cn.com/problems/daily-temperat...

  • LeetCode 739. 每日温度 | Python

    739. 每日温度 题目来源:力扣(LeetCode)https://leetcode-cn.com/proble...

  • 739. 每日温度/46. 全排列

    739. 每日温度 相关标签 : 哈希表 栈 46. 全排列 相关标签: 回溯算法

  • 739. 每日温度

    根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数。如果之后都不会升高,...

  • 739. 每日温度

    根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数。如果之后都不会升高,...

  • 739. 每日温度

    题目描述 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数。如果之后都...

  • 739. 每日温度

    题目描述: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。...

网友评论

      本文标题:739. 每日温度

      本文链接:https://www.haomeiwen.com/subject/fhcmaltx.html