-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq078_Subsets.java
More file actions
44 lines (38 loc) · 1.13 KB
/
q078_Subsets.java
File metadata and controls
44 lines (38 loc) · 1.13 KB
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
41
42
43
44
package leetcode_algorithm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Given a set of distinct integers, nums, return all possible subsets.
* <p>
* Note: The solution set must not contain duplicate subsets.
* <p>
* For example,
* If nums = [1,2,3], a solution is:
*/
public class q078_Subsets {
public static void main(String[] args) {
q078_Subsets solution = new q078_Subsets();
System.out.println(solution.subsets(new int[]{1, 2, 3}));
}
/**
* j½â·¨1 »ØËÝ·¨
*
* @param nums
* @return
*/
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
list.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}