78. Subsets
Tags: ‘Array’, ‘Backtracking’, ‘Bit Manipulation’
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Solution
标准backtrack模板 99% 100%
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
backtrack(result, new ArrayList<Integer>(), nums, 0);
return result;
}
private void backtrack(List<List<Integer>> result, List<Integer> temp, int[] nums, int start) {
// termination: no termination, we need all subsets
result.add(new ArrayList<>(temp));
for (int i = start; i < nums.length; i++) { //没有剪枝
temp.add(nums[i]);
backtrack(result, temp, nums, i+1);
temp.remove(temp.size() - 1);
}
}