leetcode332

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode332相关的知识,希望对你有一定的参考价值。

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

 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 + 堆。在中等难度的题目中,这道题目属于比较困难的了。

以上是关于leetcode332的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 332 Reconstruct Itinerary

Leetcode 332: Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

leetcode332

[LeetCode] 332. Reconstruct Itinerary Java

LeetCode332. 重新安排行程