78. subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
Input: nums = [1,2,3]
Output:
[
[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
17
18
19
20
class  {
public List<List<Integer>> subsets(int[] nums) {
int count = (int)Math.pow(2,nums.length);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
List<Integer> list = new ArrayList<>();
int temp = i;
int index = 0;
while (temp != 0) {
if ((temp & 1) == 1) {
list.add(nums[index]);
}
index++;
temp = temp >> 1;
}
result.add(list);
}
return result;
}
}