Skip to content

Commit 5594e73

Browse files
committed
Solution for 332.
1 parent e92316f commit 5594e73

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

solution/0300-0399/0332.Reconstruct Itinerary/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```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]
6077

6178
```
6279

solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
### **Python3**
5050

5151
```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]
5269

5370
```
5471

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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]

0 commit comments

Comments
 (0)