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], [] ]
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
//回溯法
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(nums);
List<Integer> tmp = new ArrayList<Integer>();
backtrack(result, tmp, nums, 0);
return result;
}
private void backtrack(List<List<Integer>> result, List<Integer> tmp, int[] nums, int start){
result.add(new ArrayList(tmp));
for (int i = start; i < nums.length; i++){
tmp.add(nums[i]);
backtrack(result, tmp, nums, i + 1);
tmp.remove(tmp.size() - 1);
}
}
}