Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
早前的写法是倒序->http://www.cppblog.com/Uriel/articles/229495.html
改了正序写法,快了不少

 1 #739
 2 #Runtime: 998 ms (Beats 89.58%)
 3 #Memory: 23.6 MB (Beats 98.27%)
 4 
 5 class Solution(object):
 6     def dailyTemperatures(self, temperatures):
 7         """
 8         :type temperatures: List[int]
 9         :rtype: List[int]
10         """
11         ans = [0] * len(temperatures)
12         stk = []
13         for i, t in enumerate(temperatures):
14             if not stk:
15                 stk.append(i)
16             else:
17                 while stk and temperatures[stk[-1]] < t:
18                     idx = stk.pop()
19                     ans[idx] = i - idx
20                 stk.append(i)
21         return ans

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理