File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -261,6 +261,43 @@ for (pair<string, int>target : targets[result[result.size() - 1]])
261
261
## 其他语言版本
262
262
263
263
### 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
+ ```
264
301
265
302
``` java
266
303
class Solution {
You can’t perform that action at this time.
0 commit comments