-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathpermutations.java
More file actions
39 lines (34 loc) · 1.27 KB
/
Copy pathpermutations.java
File metadata and controls
39 lines (34 loc) · 1.27 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
/*
The idea is to iterate each value in num, swap it with the first value, and add it to the permutations of the rest of the collection.
for example, say input is a,b,c. First, add a to (b,c) and (c,b), then add b to (a,c) and (c,a), at last, add c to (a,b) and (b,a).
*/
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
return permute(num,0);
}
public ArrayList<ArrayList<Integer>> permute(int[] num,int level){
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(level==num.length-1){
ArrayList<Integer> combo = new ArrayList<Integer>();
combo.add(num[level]);
result.add(combo);
return result;
}
for(int i=level;i<num.length;i++){
swap(num,i,level);
for(ArrayList<Integer> combo:permute(num,level+1)){
combo.add(num[level]);
result.add(combo);
}
swap(num,i,level);
}
return result;
}
public void swap(int[] num,int i, int j){
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}