78.子集
class Solution {public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> ans = new ArrayList<>();List<Integer> list = new ArrayList<>();dfs(0,nums,ans,list);return ans;}private void dfs(int cur,int[] nums,List<List<Integer>> ans,List<Integer> list){//每更新一次List,都加入结果,首先加进来的是空集ans.add(new ArrayList<>(list));for(int i = cur;i<nums.length;i++){//将当前数加入Listlist.add(nums[i]);//递归,不能重复使用当前树,下一轮从i+1开始dfs(i+1,nums,ans,list);//回溯,回退刚刚加到的数list.remove(list.size()-1);} }
}