<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

 

 

全部评论

相关推荐

白火同学:先说结论,对于一份实习简历来说,整体还是挺不错的,技术深度和广度都到位,找到一份中小厂的实习没啥问题。 再说说能优化的点吧。 1、量化结果,项目中很多工作量化一下结果给面试官的感受会更直观一些,也能体现你对应用该项技术的理解(在众多技术为什么要用它,运行性能或者说开发效率往往是一大考虑指标;而不是说大家做这种功能都用它,所以我用它)。 2、突出亮点,项目中可以从“工作职责”择一些“个人亮点”另写一块,优先去写开发过程中遇到的xx问题,使用xx技术达到xx效果,针对性去写一些疑杂难的功能,能带出你个人思考和解决的过程。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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