Given a set of distinct integers, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- 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], [] ]
public class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<Integer> item = new ArrayList<Integer>();
res.add(item);
sub(nums, 0, item);
return res;
}
private void sub(int[] nums, int index, List<Integer> input) {
for (int i = index; i < nums.length; i++) {
List<Integer> item = new ArrayList<Integer>(input);
item.add(nums[i]);
res.add(item);
sub(nums, i + 1, item);
}
}
}