Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee2993e

Browse files
0332.重新安排行程 添加Java代码
1 parent eeba178 commit ee2993e

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)

AltStyle によって変換されたページ (->オリジナル) /