LondbellLondbell Subsets

题目描述

给出一个数组,每个元素均不同,给出所有他的子集

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

1
2
3
4
5
6
7
8
9
10
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

原题地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class  {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new LinkedList<>();
int total = 2 << (nums.length) - 1;
for(int i = 0;i < total; i ++) {
List<Integer> temp = new LinkedList<>();
for(int j = 0; j < nums.length; j++) {
if((i & (1 << j)) != 0) {
temp.add(nums[j]);
}
}
list.add(temp);
}
return list;
}
}