题目链接:LeeCode78子集(Java)
题目描述:
根据图中树的模型即可解此题,将数组排序,树的子节点只能是比自己大的树即下标在自己之后的数逐层递归
public static List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> lists=new ArrayList<>();
lists.add(new ArrayList<>());
List<Integer> list=new ArrayList<>();
dfs(lists,list,nums,0);
return lists;
}
public static void dfs(List<List<Integer>> lists,List<Integer> list,int[] nums,int step){
if (step >= nums.length) {
return;
}
for (int i = step; i < nums.length; i++) {
list.add(nums[i]);
List<Integer> list1=new ArrayList<>(list);
lists.add(list1);
dfs(lists, list, nums, i+1);
list.remove(list.size()-1);
}
}