<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

 

 

全部评论

相关推荐

湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务