美文网首页
Leetcode 739 每日温度

Leetcode 739 每日温度

作者: 禾木清清 | 来源:发表于2019-11-03 22:22 被阅读0次

题目:

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

例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。

提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/daily-temperatures
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

使用递减栈的概念,详见链接:
https://www.jianshu.com/p/6bbd3653a57f
遍历数组,当元素比栈中元素都小都时候压栈(下标和数值),否则把比元素小的都弹栈。

代码

class Solution:
    def dailyTemperatures(self, T: List[int]) -> List[int]:
        stack = []
        output = [0] * len(T)
        
        for i in range(len(T)):
            while len(stack) > 0 and T[i] > stack[-1][1]:
                index, _ = stack.pop()
                output[index] = i - index
            stack.append((i, T[i]))
        
        return output
            
        

Ref:

https://jianshu.com/p/be14c1d88269

相关文章

  • LeetCode 739. 每日温度 | Python

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

  • 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 每日温度

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

  • php刷每⽇温度(LeetCode 739)

    每⽇温度(LeetCode 739) 具体代码实现: 输出温度对应结果:

  • LeetCode-739-每日温度

    LeetCode-739-每日温度 题目 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多...

  • leetcode - 739. 每日温度

    题目 解法一: 思路:输出的最后一位一定是0,从右往左遍历温度表T,如果当前温度T[i]小于上一次温度T[i + ...

  • leetcode_739 每日温度

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

网友评论

      本文标题:Leetcode 739 每日温度

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