Skip to content

Commit 6553b5b

Browse files
authored
Merge pull request #2 from zhicheng-lee/zhicheng-lee-patch-1
0332.重新安排行程 添加Java代码
2 parents eeba178 + ee2993e commit 6553b5b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

problems/0332.重新安排行程.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,43 @@ for (pair<string, int>target : targets[result[result.size() - 1]])
261261
## 其他语言版本
262262

263263
### java
264+
265+
```java
266+
class Solution {
267+
private LinkedList<String> res;
268+
private LinkedList<String> path = new LinkedList<>();
269+
270+
public List<String> findItinerary(List<List<String>> tickets) {
271+
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
272+
path.add("JFK");
273+
boolean[] used = new boolean[tickets.size()];
274+
backTracking((ArrayList) tickets, used);
275+
return res;
276+
}
277+
278+
public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
279+
if (path.size() == tickets.size() + 1) {
280+
res = new LinkedList(path);
281+
return true;
282+
}
283+
284+
for (int i = 0; i < tickets.size(); i++) {
285+
if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) {
286+
path.add(tickets.get(i).get(1));
287+
used[i] = true;
288+
289+
if (backTracking(tickets, used)) {
290+
return true;
291+
}
292+
293+
used[i] = false;
294+
path.removeLast();
295+
}
296+
}
297+
return false;
298+
}
299+
}
300+
```
264301

265302
```java
266303
class Solution {

0 commit comments

Comments
 (0)