-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathpermutation.java
More file actions
36 lines (27 loc) · 925 Bytes
/
permutation.java
File metadata and controls
36 lines (27 loc) · 925 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 static void fun(List<List<Integer>> res, List<Integer> ds, boolean[] vis, int[] arr){
if(ds.size() == arr.length){
res.add(new ArrayList<>(ds));
return;
}
for(int i=0;i<arr.length;i++){
if(!vis[i]){
vis[i] = true;
ds.add(arr[i]);
fun(res, ds, vis, arr);
ds.remove(ds.size()-1);
vis[i] = false;
}
}
}
public static void main(String[] args){
int[] arr = {1,2,3};
List<List<Integer>> res = new ArrayList<>();
boolean[] vis = new boolean[arr.length];
fun(res, new ArrayList<>(), vis, arr);
System.out.println(res);
}
}
// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/