1
1
package backtracking ;
2
2
3
+ import javax .annotation .Resource ;
3
4
import java .util .ArrayList ;
4
5
import java .util .Arrays ;
5
6
import java .util .List ;
@@ -10,7 +11,10 @@ public class Subsets {
10
11
public static void main (String [] args ) {
11
12
int [] nums = new int []{1 ,2 ,3 };
12
13
Subsets obj = new Subsets ();
13
- List <List <Integer >> resultList = obj .subsets (nums );
14
+ //List<List<Integer>> resultList = obj.subsets(nums);
15
+ //List<List<Integer>> resultList = obj.subsetsWithRecursion(nums);
16
+ //List<List<Integer>> resultList = obj.subsetsWithBacktrack(nums);
17
+ List <List <Integer >> resultList = obj .subsetsWithBinarySorted (nums );
14
18
System .out .println (Arrays .toString (resultList .toArray ()));
15
19
}
16
20
@@ -37,4 +41,70 @@ private void dfs(int[] nums, List<List<Integer>> resultList, List<Integer> list,
37
41
list .remove (list .size () - 1 );
38
42
dfs (nums , resultList , list , index + 1 );
39
43
}
44
+
45
+
46
+ public List <List <Integer >> subsetsWithRecursion (int [] nums ) {
47
+ List <List <Integer >> outputList = new ArrayList <List <Integer >>();
48
+ outputList .add (new ArrayList <Integer >());
49
+ for (int num : nums ) {
50
+ List <List <Integer >> newList = new ArrayList <List <Integer >>();
51
+ for (List <Integer > list : outputList ) {
52
+ newList .add (new ArrayList <Integer >(list ) {{ add (num ); }});
53
+ }
54
+
55
+ for (List <Integer > list : newList ) {
56
+ outputList .add (list );
57
+ }
58
+ }
59
+
60
+ return outputList ;
61
+ }
62
+
63
+ public List <List <Integer >> subsetsWithBacktrack (int [] nums ) {
64
+ List <List <Integer >> resultList = new ArrayList <List <Integer >>();
65
+ for (int len = 0 ; len <= nums .length ; len ++) {
66
+ // backtrack
67
+ backtrack (nums , resultList , new ArrayList <Integer >(), 0 , len );
68
+ }
69
+
70
+ return resultList ;
71
+ }
72
+
73
+ private void backtrack (int [] nums , List <List <Integer >> resultList , List <Integer > list , int first , int len ) {
74
+ // exit
75
+ if (list .size () == len ) {
76
+ resultList .add (new ArrayList <Integer >(list ));
77
+ return ;
78
+ }
79
+
80
+ if (first == nums .length ) {
81
+ return ;
82
+ }
83
+ list .add (nums [first ]);
84
+ backtrack (nums , resultList , list , first + 1 , len );
85
+ list .remove (list .size () - 1 );
86
+ backtrack (nums , resultList , list , first + 1 , len );
87
+ }
88
+
89
+ public List <List <Integer >> subsetsWithBinarySorted (int [] nums ) {
90
+ List <List <Integer >> resultList = new ArrayList <List <Integer >>();
91
+ int n = nums .length ;
92
+
93
+ for (int i = (int )Math .pow (2 , n ); i < (int )Math .pow (2 , n + 1 ); i ++) {
94
+ // generate bitmask, from 0..00 to 1..11
95
+ String bitmask = Integer .toBinaryString (i ).substring (1 );
96
+
97
+ // append subset corresponding to that bitmask
98
+ List <Integer > list = new ArrayList <Integer >();
99
+ for (int k = 0 ; k < n ; k ++) {
100
+ if (bitmask .charAt (k ) == '1' ) {
101
+ list .add (nums [k ]);
102
+ }
103
+ }
104
+
105
+ resultList .add (list );
106
+ }
107
+
108
+ return resultList ;
109
+ }
40
110
}
0 commit comments