Enumerate the power set
PowerSetAlternative.java
public static void generatePowerSet(List<Integer> S) {
Deque<Integer> res = new LinkedList<>();
generatePowerSetHelper(S, 0, res);
}
private static void generatePowerSetHelper(List<Integer> S, int idx,
Deque<Integer> res) {
if (!res.isEmpty()) {
// Print the subset.
Iterator<Integer> iterator = res.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next());
if (iterator.hasNext()) {
System.out.print(",");
}
}
}
System.out.println();
for (int i = idx; i < S.size(); ++i) {
res.offerLast(S.get(i));
generatePowerSetHelper(S, i + 1, res);
res.pollLast();
}
}