-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsets.java
37 lines (30 loc) · 891 Bytes
/
Subsets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.sean.backtracking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/***
* 78. Subsets
*/
public class Subsets {
public List<List<Integer>> subsets(int[] nums) {
lookup(nums, 0, new LinkedList<>());
for (List<Integer> lt: allSets){
System.out.println(Arrays.toString(lt.stream().mapToInt(value -> value).toArray()));
}
return allSets;
}
List<List<Integer>> allSets = new ArrayList<>();
void lookup(int[] nums, int pos, LinkedList<Integer> current) {
allSets.add(new ArrayList<>(current));
int len = nums.length;
if (pos >= len) {
return;
}
for (int i = pos; i < len; i++) {
current.add(nums[i]);
lookup(nums, i + 1, current);
current.removeLast();
}
}
}