forked from LjyYano/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL078_Subsets.java
More file actions
49 lines (34 loc) · 837 Bytes
/
L078_Subsets.java
File metadata and controls
49 lines (34 loc) · 837 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
41
42
43
44
45
46
47
48
49
package LeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class L078_Subsets {
int target;// 次数
Integer[] stack;// 存储每次排列
List<List<Integer>> rt;// 存储结果
public void search(int p, int[] nums) {
// 若长度为k,则stack是其中一个结果,保存结果
if (p == target) {
rt.add(new ArrayList<Integer>(Arrays.asList(stack)));
return;
}
for (int i = 0; i < nums.length; i++) {
if (p > 0 && nums[i] <= stack[p - 1]) {
continue;
}
stack[p] = nums[i];
search(p + 1, nums);
}
}
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
rt = new ArrayList<List<Integer>>();
// 分别做0~num.length长度的组合
for (int i = 0; i <= nums.length; i++) {
target = i;
stack = new Integer[i];
search(0, nums);
}
return rt;
}
}