-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
34 lines (28 loc) · 841 Bytes
/
Permutations.java
File metadata and controls
34 lines (28 loc) · 841 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
import java.util.ArrayList;
import java.util.List;
public class Permutations {
static void permute(List<Integer> arr, int k, ArrayList<int[]> permutations){
for(int i = k; i < arr.size(); i++){
java.util.Collections.swap(arr, i, k);
permute(arr, k+1, permutations);
java.util.Collections.swap(arr, k, i);
}
if (k == arr.size() -1){
permutations.add(ListToArray(arr));
}
}
public static int[] ListToArray(List<Integer> list) {
int i = 0;
int[] array = new int[list.size()];
for (int x : list) {
array[i] = x;
i++;
}
return array;
}
public static ArrayList<int[]> getPermutations(ArrayList<Integer> list) {
ArrayList<int[]> permutations = new ArrayList<int[]>();
permute(list, 0, permutations);
return permutations;
}
}