forked from reedfan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN078Subsets.java
More file actions
90 lines (74 loc) · 2.09 KB
/
Copy pathN078Subsets.java
File metadata and controls
90 lines (74 loc) · 2.09 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package leetcode.digui;
import java.util.ArrayList;
import java.util.List;
/**
* 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
* <p>
* 说明:解集不能包含重复的子集。
* <p>
* 示例:
* <p>
* 输入: nums = [1,2,3]
* 输出:
* [
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
* <p>
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/subsets
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class N078Subsets {
private ArrayList<List<Integer>> res;
public List<List<Integer>> subsets(int[] nums) {
res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
List<Integer> list = new ArrayList<>();
res.add(new ArrayList<>());
generate(0, nums, list);
return res;
}
private void generate(int start, int[] nums, List<Integer> list) {
//终止条件
if (start >= nums.length) {
return;
}
//选择放入次数
list.add(nums[start]);
res.add(new ArrayList<>(list));
generate(start + 1, nums, list);
//选择不放入次数
list.remove(list.size()-1);
generate(start + 1, nums, list);
}
List<List<Integer>> lists = new ArrayList<>();
public List<List<Integer>> subsets1(int[] nums) {
if(nums == null || nums.length ==0){
return lists;
}
List<Integer> list = new ArrayList<>();
process(list, nums, 0);
return lists;
}
private void process(List<Integer>list, int[] nums, int start){
lists.add(new ArrayList(list));
for(int i = start; i < nums.length; i++){
list.add(nums[i]);
process(list, nums, i+1);
list.remove(list.size()-1);
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> lists = (new N078Subsets()).subsets(nums);
}
}