-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.java
More file actions
31 lines (27 loc) · 843 Bytes
/
Copy pathpermutation.java
File metadata and controls
31 lines (27 loc) · 843 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
import java.util.*;
class permutation{
public static void main(String[] args){
int[] arr = {1,2,3,4};
List<List<Integer>> list = new ArrayList<>();
int[] flag = new int[arr.length];
getAll(arr, flag, list, new ArrayList<Integer>());
for(List a: list){
System.out.println(a);
}
}
public static void getAll(int[] nums, int[] flag, List<List<Integer>> list, List<Integer> li){
if(li.size() == nums.length){
list.add(new ArrayList<Integer>(li));
return;
}
for(int i = 0; i<nums.length; i++){
if(flag[i] == 1)
continue;
li.add(nums[i]);
flag[i] = 1;
getAll(nums, flag, list,li);
li.remove(li.size() - 1);
flag[i] = 0;
}
}
}