回溯法 | 78.子集
class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { dfs(nums,0,new ArrayList<>()); res.add(new ArrayList<>()); return res; } public void dfs(int[] nums, int idx, List<Integer> list){ if(idx>=nums.length){ return; } for(int i=idx;i<nums.length;i++){ list.add(nums[i]); res.add(new ArrayList<>(list)); dfs(nums,i+1,list); list.remove(list.size()-1); } } }