根据往后 n 天的天气预报,计算每一天需要等待几天才会出现一次更高的气温,如果往后都没有更高的气温,则用 0 补位。
例如往后三天的气温是 [1,2,3] , 则输出 [1,1,0]
数据范围: ,每天的温度会满足
[1,2,3]
[1,1,0]
[2,4,5,9,10,0,9]
[1,1,1,1,0,1,0]
[3,1,4]
[2,1,0]
class Solution: def temperatures(self , temperatures: List[int]) -> List[int]: # write code here stack, res = [], [0] * len(temperatures) for i in range(len(temperatures)-1, -1, -1): while stack and temperatures[i] >= temperatures[stack[-1]]: stack.pop() if not stack: res[i] = 0 else: res[i] = stack[-1] - i stack.append(i) return res