Skip to content

Commit fef92e0

Browse files
authored
feat: add Java solution to lc problem: No.332 (doocs#1624)
1 parent 433bf34 commit fef92e0

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,37 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```java
68-
68+
class Solution {
69+
void dfs(Map<String, Queue<String>> adjLists, List<String> ans, String curr) {
70+
Queue<String> neighbors = adjLists.get(curr);
71+
if (neighbors == null) {
72+
ans.add(curr);
73+
return;
74+
}
75+
while (!neighbors.isEmpty()) {
76+
String neighbor = neighbors.poll();
77+
dfs(adjLists, ans, neighbor);
78+
}
79+
ans.add(curr);
80+
return;
81+
}
82+
83+
public List<String> findItinerary(List<List<String>> tickets) {
84+
Map<String, Queue<String>> adjLists = new HashMap<>();
85+
for (List<String> ticket : tickets) {
86+
String from = ticket.get(0);
87+
String to = ticket.get(1);
88+
if (!adjLists.containsKey(from)) {
89+
adjLists.put(from, new PriorityQueue<>());
90+
}
91+
adjLists.get(from).add(to);
92+
}
93+
List<String> ans = new ArrayList<>();
94+
dfs(adjLists, ans, "JFK");
95+
Collections.reverse(ans);
96+
return ans;
97+
}
98+
}
6999
```
70100

71101
### **...**

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,37 @@
5555
### **Java**
5656

5757
```java
58-
58+
class Solution {
59+
void dfs(Map<String, Queue<String>> adjLists, List<String> ans, String curr) {
60+
Queue<String> neighbors = adjLists.get(curr);
61+
if (neighbors == null) {
62+
ans.add(curr);
63+
return;
64+
}
65+
while (!neighbors.isEmpty()) {
66+
String neighbor = neighbors.poll();
67+
dfs(adjLists, ans, neighbor);
68+
}
69+
ans.add(curr);
70+
return;
71+
}
72+
73+
public List<String> findItinerary(List<List<String>> tickets) {
74+
Map<String, Queue<String>> adjLists = new HashMap<>();
75+
for (List<String> ticket : tickets) {
76+
String from = ticket.get(0);
77+
String to = ticket.get(1);
78+
if (!adjLists.containsKey(from)) {
79+
adjLists.put(from, new PriorityQueue<>());
80+
}
81+
adjLists.get(from).add(to);
82+
}
83+
List<String> ans = new ArrayList<>();
84+
dfs(adjLists, ans, "JFK");
85+
Collections.reverse(ans);
86+
return ans;
87+
}
88+
}
5989
```
6090

6191
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
void dfs(Map<String, Queue<String>> adjLists, List<String> ans, String curr) {
3+
Queue<String> neighbors = adjLists.get(curr);
4+
if (neighbors == null) {
5+
ans.add(curr);
6+
return;
7+
}
8+
while (!neighbors.isEmpty()) {
9+
String neighbor = neighbors.poll();
10+
dfs(adjLists, ans, neighbor);
11+
}
12+
ans.add(curr);
13+
return;
14+
}
15+
16+
public List<String> findItinerary(List<List<String>> tickets) {
17+
Map<String, Queue<String>> adjLists = new HashMap<>();
18+
for (List<String> ticket : tickets) {
19+
String from = ticket.get(0);
20+
String to = ticket.get(1);
21+
if (!adjLists.containsKey(from)) {
22+
adjLists.put(from, new PriorityQueue<>());
23+
}
24+
adjLists.get(from).add(to);
25+
}
26+
List<String> ans = new ArrayList<>();
27+
dfs(adjLists, ans, "JFK");
28+
Collections.reverse(ans);
29+
return ans;
30+
}
31+
}

0 commit comments

Comments
 (0)