-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.java
More file actions
36 lines (28 loc) · 986 Bytes
/
Permutation.java
File metadata and controls
36 lines (28 loc) · 986 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
35
36
import java.util.*;
public class Permutation {
public List<List<Integer>> permute(int[] arr){
List<List<Integer>> result= new ArrayList<>();
backtrack(result, new ArrayList<>(), arr);
return result;
}
private void backtrack(List<List<Integer>> result, ArrayList<Integer> temp,int[] arr){
if(temp.size() == arr.length){
result.add(new ArrayList<>(temp));
return;
}
for(int number : arr){
if(temp.contains(number)){
continue;
}
temp.add(number);
backtrack(result,temp,arr);
temp.remove(temp.size()-1);
}
}
public static void main(String[] args) {
int[] arr= {1,2,3,4};
Permutation obj= new Permutation();
List<List<Integer>> resulting_array= obj.permute(arr);
System.out.println(resulting_array);
}
}