没能做出来,参考别人的:

 1 from heapq import heapify, heappush, heappop
 2 
 3 class Solution:
 4     def findItinerary(self, tickets: List[List[str]]) -> List[str]:
 5         graph = collections.defaultdict(list)
 6         for src, des in tickets:
 7             heappush(graph[src], des)
 8         res = []
 9         self.DFS('JFK', graph, res)
10         return res[::-1]
11 
12     def DFS(self, src, graph, res):
13         while graph[src]:
14             self.DFS(heappop(graph[src]), graph, res)
15         res += [src]
16         return

参考地址:https://leetcode.com/problems/reconstruct-itinerary/discuss/489268/Clean-Python-solution-beats-99-in-time-100-in-space

算法思路:深度优先遍历DFS + 堆。在中等难度的题目中,这道题目属于比较困难的了。

posted on 2020-04-08 20:18  Sempron2800+  阅读(188)  评论(0编辑  收藏  举报