-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (26 loc) · 920 Bytes
/
Copy pathsolution.java
File metadata and controls
34 lines (26 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
private static void backtrack(int[] arr, boolean[] used,
ArrayList<Integer> curr,
ArrayList<ArrayList<Integer>> result) {
if (curr.size() == arr.length) {
result.add(new ArrayList<>(curr));
return;
}
for (int i = 0; i < arr.length; i++) {
if (used[i])
continue;
used[i] = true;
curr.add(arr[i]);
backtrack(arr, used, curr, result);
// Backtrack
curr.remove(curr.size() - 1);
used[i] = false;
}
}
public static ArrayList<ArrayList<Integer>> permuteDist(int[] arr) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
boolean[] used = new boolean[arr.length];
backtrack(arr, used, new ArrayList<>(), result);
return result;
}
}