-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
44 lines (40 loc) · 1.23 KB
/
Permutations.java
File metadata and controls
44 lines (40 loc) · 1.23 KB
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
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
public class Permutations {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
int len = num.length;
if (len == 0) {
return result;
}
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(num[0]);
result.add(arr);
for(int i = 1; i < len; i++) {
//printMat(result);
ArrayList<ArrayList<Integer>> temp = new ArrayList<ArrayList<Integer>>();
for (int j = 0; j < result.size(); j++) {
for (int k = 0; k <= result.get(j).size(); k++) {
ArrayList<Integer> cur = (ArrayList<Integer>) result.get(j).clone();
cur.add(k, num[i]);
temp.add(cur);
//printMat(temp);
}
}
result = temp;
}
return result;
}
public void printMat(ArrayList<ArrayList<Integer>> result) {
for (ArrayList<Integer> arr: result) {
for (Integer x: arr) {
System.out.print(x + " ");
}
System.out.print("\n");
}
}
public void testPermute() {
int[] num = {1,2,3};
ArrayList<ArrayList<Integer>> result = permute(num);
printMat(result);
}
}