<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

 

 

全部评论

相关推荐

建信金科 软件开发岗 16k 双非硕
点赞 评论 收藏
分享
贺兰星辰:不要漏个人信息,除了简历模板不太好以外你这个个人简介是不是太夸大了...
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务