-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.子集.java
More file actions
41 lines (36 loc) · 1001 Bytes
/
78.子集.java
File metadata and controls
41 lines (36 loc) · 1001 Bytes
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
38
39
40
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/*
* @lc app=leetcode.cn id=78 lang=java
*
* [78] 子集
*/
// @lc code=start
class Solution {
private List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
boolean[] cache = new boolean[21];
for (int num : nums) {
cache[num + 10] = true;
}
dfs(cache, 0, new LinkedList<>());
result.add(Collections.emptyList());
return result;
}
private void dfs(boolean[] cache, int index, LinkedList<Integer> list) {
if (index == cache.length) {
if (!list.isEmpty())
result.add(new ArrayList<>(list));
return;
}
dfs(cache, index + 1, list);
if (cache[index]) {
list.addLast(index - 10);
dfs(cache, index + 1, list);
list.removeLast();
}
}
}
// @lc code=end