-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutate2.java
More file actions
58 lines (46 loc) · 1.43 KB
/
Permutate2.java
File metadata and controls
58 lines (46 loc) · 1.43 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
import java.util.List;
public class Permutate2 {
public static void main(String args[]){
int arr[] = {1,2,3};
List<List<Integer>> ans = subset(arr);
for(List<Integer> list : ans){
System.out.print(list);
}
System.out.println();
//duplicates?
int arr2[] = {1,2,2};
List<List<Integer>> ans2 = subset2(arr2);
for(List<Integer> list : ans2){
System.out.print(list);
}
}
static List<List<Integer>> subset(int arr[]){
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
for(int num : arr){
int n = outer.size();
for(int i=0;i<n;i++){
List<Integer> internal = new ArrayList<>(outer.get(i));
internal.add(num);
outer.add(internal);
}
}
return outer;
}
static List<List<Integer>> subset2(int arr[]){
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
for(int num : arr){
int n = outer.size();
for(int i=0;i<n;i++){
List<Integer> internal = new ArrayList<>(outer.get(i));
internal.add(num);
if(!outer.contains(internal)){
outer.add(internal);
}
}
}
return outer;
}
}