<span>leetcode-739 Daily Temperatures</span>
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead. For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0]. Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
这道题不能直接暴力求解,会超时(我试过了TnT),需要利用到栈后进先出的特性。
1 class Solution: 2 def dailyTemperatures(self, T: List[int]) -> List[int]: 3 stack = [] 4 arr = [0] * len(T) 5 stack.append(0) 6 for i in range(1,len(T)): 7 while len(stack) > 0: 8 temp = stack.pop() 9 if T[i] > T[temp]: 10 arr[temp] = i - temp 11 else: 12 stack.append(temp) 13 break 14 stack.append(i) 15 return arr