Given an array nums
of unique integers, return all possible subsets of nums
.
The solution set must not contain duplicate subsets. You may return the solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [7]
Output: [[],[7]]
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
public List> subsets(int[] nums) {
List> res = new ArrayList<>();
List subset = new ArrayList<>();
dfs(nums, 0, subset, res);
return res;
}
private void dfs(int[] nums, int i, List subset, List> res) {
if (i >= nums.length) {
res.add(new ArrayList<>(subset));
return;
}
subset.add(nums[i]);
dfs(nums, i + 1, subset, res);
subset.remove(subset.size() - 1);
dfs(nums, i + 1, subset, res);
}