python-leetcode-每日温度

739. 每日温度 - 力扣(LeetCode)

python-leetcode-每日温度_第1张图片

python-leetcode-每日温度_第2张图片

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        answer = [0] * n
        stack = []  # 存储索引
        
        for i, temp in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < temp:
                prev_index = stack.pop()
                answer[prev_index] = i - prev_index
            stack.append(i)
        
        return answer

你可能感兴趣的:(python,leetcode,算法,职场和发展)