-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackTrack_Permutation.java
More file actions
35 lines (33 loc) · 1.03 KB
/
BackTrack_Permutation.java
File metadata and controls
35 lines (33 loc) · 1.03 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
import java.util.*;
public class BackTrack_Permutation {
public static List<List<Integer>> generate(int [] arr){
List<List<Integer>> result = new ArrayList<>();
backtrack(result,0,arr);
return result;
}
public static void backtrack(List<List<Integer>> result , int start, int[] num){
if(start == num.length){
List<Integer> temp = new ArrayList<>();
for(int i: num)
temp.add(i);
result.add(temp);
}
for(int i = start;i<num.length;i++){
swap(num, start, i);
backtrack(result, start + 1, num);
swap(num, start, i);
}
}
public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
List<List<Integer>> permutations = generate(arr);
for(List<Integer> perm : permutations) {
System.out.println(perm);
}
}
}