题目描述
题目难度:Medium
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],
[]
]
AC代码
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> resList = new ArrayList<>();
if(nums == null || nums.length == 0) return resList;
backtracing(nums, 0, new ArrayList<Integer>(), resList);
return resList;
}
private void backtracing(int[] nums, int start, List<Integer> list, List<List<Integer>> resList){
resList.add(new ArrayList<Integer>(list));
for(int i = start; i < nums.length; i++){
list.add(nums[i]);
backtracing(nums, i + 1, list, resList);
list.remove(list.size() - 1);
}
}
}