fork(7) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(helfer(new String[]{"A", "u", "D"}, 0));
  13. }
  14. static <T> List<List<T>> helfer(T[] s, int idx) {
  15. // Rückgabe pms ist Potenzmenge von s ab index idx
  16. List<List<T>> pms = new ArrayList<List<T>>();
  17. if (idx >= s.length) {
  18. // Basisfall
  19. pms.add(new ArrayList<T>());
  20. } else {
  21. // aktuelles Kopfelement bestimmen
  22. T kopf = s[idx];
  23.  
  24. // Potenzmenge der Restliste bestimmen
  25. List<List<T>> potRest = helfer(s, idx + 1);
  26.  
  27. // Ergebnisse zusammenführen
  28. for (List<T> ohneKopf : potRest) {
  29. List<T> mitKopf = new ArrayList<>(ohneKopf); // *nocH* ohne Kopf
  30. mitKopf.add(kopf);
  31.  
  32. pms.add(ohneKopf);
  33. pms.add(mitKopf);
  34. }
  35. }
  36. return pms;
  37. }
  38. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
[[], [A], [u], [u, A], [D], [D, A], [D, u], [D, u, A]]