File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed
solution/0300-0399/0332.Reconstruct Itinerary Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 57
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
58
59
59
``` python
60
+ class Solution :
61
+ def findItinerary (self , tickets : List[List[str ]]) -> List[str ]:
62
+ graph = defaultdict(list )
63
+
64
+ for src, dst in sorted (tickets, reverse = True ):
65
+ graph[src].append(dst)
66
+
67
+ itinerary = []
68
+
69
+ def dfs (airport ):
70
+ while graph[airport]:
71
+ dfs(graph[airport].pop())
72
+ itinerary.append(airport)
73
+
74
+ dfs(" JFK" )
75
+
76
+ return itinerary[::- 1 ]
60
77
61
78
```
62
79
Original file line number Diff line number Diff line change 49
49
### ** Python3**
50
50
51
51
``` python
52
+ class Solution :
53
+ def findItinerary (self , tickets : List[List[str ]]) -> List[str ]:
54
+ graph = defaultdict(list )
55
+
56
+ for src, dst in sorted (tickets, reverse = True ):
57
+ graph[src].append(dst)
58
+
59
+ itinerary = []
60
+
61
+ def dfs (airport ):
62
+ while graph[airport]:
63
+ dfs(graph[airport].pop())
64
+ itinerary.append(airport)
65
+
66
+ dfs(" JFK" )
67
+
68
+ return itinerary[::- 1 ]
52
69
53
70
```
54
71
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findItinerary (self , tickets : List [List [str ]]) -> List [str ]:
3
+ graph = defaultdict (list )
4
+
5
+ for src , dst in sorted (tickets , reverse = True ):
6
+ graph [src ].append (dst )
7
+
8
+ itinerary = []
9
+
10
+ def dfs (airport ):
11
+ while graph [airport ]:
12
+ dfs (graph [airport ].pop ())
13
+ itinerary .append (airport )
14
+
15
+ dfs ("JFK" )
16
+
17
+ return itinerary [::- 1 ]
You can’t perform that action at this time.
0 commit comments