心得:常规回溯的组合版,加入一个参数,根据不同的长度
得出子集。
1 class Solution { 2 public List<List<Integer>> subsets(int[] nums) { 3 List<List<Integer>> list=new ArrayList<>(); 4 if(nums==null||nums.length==0) 5 return list; 6 Arrays.sort(nums); 7 for(int i=0;i<=nums.length;i++) 8 rec(new ArrayList(),list,i,0,nums); 9 return list; 10 } 11 public void rec(ArrayList tmp,List list,int size,int index,int[] nums) 12 { 13 if(size==0) 14 { 15 list.add(new ArrayList(tmp)); 16 } 17 for(int i=index;i<nums.length;i++) 18 { 19 tmp.add(nums[i]); 20 rec(tmp,list,size-1,i+1,nums); 21 tmp.remove(tmp.size()-1); 22 } 23 } 24 }