Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <A> List<List<A>> product(List<A> l) {
- if (l.isEmpty())
- return Collections.singletonList(Collections.emptyList());
- A hd = l.get(0);
- List<List<A>> res = new ArrayList<>();
- List<List<A>> rec = product(l.subList(1, l.size()));
- // Combinations with hd
- for (List<A> r : rec) {
- List<A> hd_r = new ArrayList<>();
- hd_r.add(hd);
- hd_r.addAll(r);
- res.add(hd_r);
- }
- // Combinations without hd
- res.addAll(rec);
- return res;
- }
- public static void main(String[] args) {
- List<Integer> set = new ArrayList<>();
- set.add(0);
- set.add(1);
- set.add(2);
- set.add(3);
- List<List<Integer>> product = product(set);
- System.out.println(product);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement