Skip to content

Commit 63adcf1

Browse files
committed
chapter 13
1 parent 2632895 commit 63adcf1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/chapter13/Main.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package chapter13;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class Main {
9+
10+
public static List<List<Integer>> subsets(List<Integer> list) {
11+
if(list.isEmpty()) {
12+
List<List<Integer>> ans = new ArrayList<>();
13+
ans.add(Collections.emptyList());
14+
return ans;
15+
}
16+
Integer first = list.get(0);
17+
List<Integer> rest = list.subList(1, list.size());
18+
List<List<Integer>> subans = subsets(rest);
19+
List<List<Integer>> subans2 = insertAll(first, subans);
20+
return concat(subans, subans2);
21+
}
22+
23+
public static List<List<Integer>> insertAll(Integer first, List<List<Integer>> subans) {
24+
List<List<Integer>> result = new ArrayList<>();
25+
for(List<Integer> list : subans) {
26+
List<Integer> copyList = new ArrayList<>();
27+
copyList.add(first);
28+
copyList.addAll(list);
29+
result.add(copyList);
30+
}
31+
return result;
32+
}
33+
34+
public static List<List<Integer>> concat(List<List<Integer>> first, List<List<Integer>> second) {
35+
List<List<Integer>> result = new ArrayList<>(first);
36+
result.addAll(second);
37+
return result;
38+
}
39+
40+
public static void main(String[] args) {
41+
List<Integer> list = Arrays.asList(new Integer[] {1,2,3,4});
42+
System.out.println(subsets(list));
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)