-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_78_9.java
More file actions
39 lines (28 loc) · 926 Bytes
/
LeetCode_78_9.java
File metadata and controls
39 lines (28 loc) · 926 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
package com.github.lifelab.leetcode.problemset;
import java.util.LinkedList;
import java.util.List;
/**
* 子集 @see https://leetcode-cn.com/problems/subsets/
*
* @author Weichao Li (liweichao0102@gmail.com)
* @since 2019-06-30
*/
public class Solution78 {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new LinkedList<>();
traverse(nums, 0, list, new LinkedList<>());
return list;
}
private void traverse(int[] nums, int index, List<List<Integer>> list, List<Integer> currentList) {
// terminator
if (nums.length == index) {
list.add(currentList);
return;
}
// process & drill down
List<Integer> target = new LinkedList<>(currentList);
target.add(nums[index]);
traverse(nums, index + 1, list, currentList);
traverse(nums, index + 1, list, target);
}
}