Skip to content

Commit

Permalink
0332.重新安排行程 添加Java代码
Browse files Browse the repository at this point in the history
  • Loading branch information
fengxiuyang committed Apr 9, 2022
1 parent eeba178 commit ee2993e
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions problems/0332.重新安排行程.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,43 @@ for (pair<string, int>target : targets[result[result.size() - 1]])
## 其他语言版本

### java

```java
class Solution {
private LinkedList<String> res;
private LinkedList<String> path = new LinkedList<>();

public List<String> findItinerary(List<List<String>> tickets) {
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
path.add("JFK");
boolean[] used = new boolean[tickets.size()];
backTracking((ArrayList) tickets, used);
return res;
}

public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
if (path.size() == tickets.size() + 1) {
res = new LinkedList(path);
return true;
}

for (int i = 0; i < tickets.size(); i++) {
if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) {
path.add(tickets.get(i).get(1));
used[i] = true;

if (backTracking(tickets, used)) {
return true;
}

used[i] = false;
path.removeLast();
}
}
return false;
}
}
```

```java
class Solution {
Expand Down

0 comments on commit ee2993e

Please sign in to comment.