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:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Solution
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
subSets(nums, 0, result);
return result;
}
public void subSets(int[] nums, int start, List<List<Integer>> result) {
if (start >= nums.length) {
return;
}
List<List<Integer>> temp = new ArrayList<>();
result.forEach(item -> {
List<Integer> cur = new ArrayList<Integer>(item);
cur.add(nums[start]);
temp.add(cur);
});
result.addAll(temp);
subSets(nums, start + 1, result);
}
}