tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
23
Task/Power-set/Java/power-set-2.java
Normal file
23
Task/Power-set/Java/power-set-2.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
public static <T> List<List<T>> powerset(Collection<T> list) {
|
||||
List<List<T>> ps = new ArrayList<List<T>>();
|
||||
ps.add(new ArrayList<T>()); // add the empty set
|
||||
|
||||
// for every item in the original list
|
||||
for (T item : list) {
|
||||
List<List<T>> newPs = new ArrayList<List<T>>();
|
||||
|
||||
for (List<T> subset : ps) {
|
||||
// copy all of the current powerset's subsets
|
||||
newPs.add(subset);
|
||||
|
||||
// plus the subsets appended with the current item
|
||||
List<T> newSubset = new ArrayList<T>(subset);
|
||||
newSubset.add(item);
|
||||
newPs.add(newSubset);
|
||||
}
|
||||
|
||||
// powerset is now powerset of list.subList(0, list.indexOf(item)+1)
|
||||
ps = newPs;
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue