File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ package AlgoExpert_Medium ;
2
+
3
+ import java .util .List ;
4
+ import java .util .ArrayList ;
5
+
6
+ public class PowerSet {
7
+
8
+ public static void main (String [] args ) {
9
+ List <Integer > array = new ArrayList <>() ;
10
+ array .add (1 ) ; array .add (2 ) ; array .add (3 ) ;
11
+ System .out .println ( powerset ( array ) ) ;
12
+ }
13
+
14
+ public static List <List <Integer >> powerset (List <Integer > array ) {
15
+
16
+ List <List <Integer >> list = new ArrayList <List <Integer >>() ;
17
+ list .add (new ArrayList <>() ) ;
18
+
19
+ List <Integer > current = new ArrayList <>() ;
20
+
21
+ return pset (array , current , list ) ;
22
+ }
23
+
24
+ public static List <List <Integer >> pset ( List <Integer > array , List <Integer > current , List <List <Integer >> list ) {
25
+
26
+
27
+ for (int i =0 ; i <array .size (); i ++) {
28
+
29
+ int currentNumber = array .get (i ) ;
30
+
31
+ if (!current .contains (currentNumber ) ){ // Constraints.
32
+
33
+ if (current .size ()<1 || currentNumber > current .get (current .size ()-1 ) ) { // Nice use of ||
34
+ current .add (currentNumber ) ; // Choose
35
+ list .add (new ArrayList <>(current )) ;
36
+ pset (array , current , list ) ; // Explore
37
+ current .remove (current .size ()-1 ) ; // Unchoose
38
+ }
39
+ }
40
+ }
41
+
42
+ return list ;
43
+
44
+ }
45
+
46
+ }
You can’t perform that action at this time.
0 commit comments