leetcode-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
class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ret = new ArrayList<>();
dfs(nums, 0, new ArrayList<>(), ret);
return ret;
}

void dfs(int[] nums, int idx, List<Integer> path, List<List<Integer>> ret)
{
ret.add(path);
for(int i=idx; i<nums.length; i++)
{
List<Integer> p = new ArrayList<>(path);
p.add(nums[i]);
dfs(nums, i+1, p, ret);
}
}
}
Donate? comment?