Given a set of distinct integers, nums, return all possible subsets.
Note:
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
if(nums == null) {
return null;
}
List<List<Integer>> sols = new ArrayList<List<Integer>>();
sols.add(new ArrayList<Integer>());
Arrays.sort(nums);
helper(nums, 0, new ArrayList<Integer>(), sols);
return sols;
}
private void helper(int[] nums, int start, List<Integer> sol, List<List<Integer>> sols) {
for(int i = start; i < nums.length; i++) {
sol.add(nums[i]);
sols.add(new ArrayList<Integer>(sol));
helper(nums, i + 1, sol, sols);
sol.remove(sol.size() - 1);
}
}
}