腾讯五十题No.22 子集

题目链接
看来还得读写几个题才会有感觉

力扣的大佬是真的多

class Solution {
    
    List<List<Integer>> res;
    List<Integer> path;
    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<>();
        path = new ArrayList<>();
        res.add(new ArrayList<>(path));
        dfs(nums,0);
        return res;
    }
    public void dfs(int[] nums,int start){
        //i每次的值都为上一次递归中加1下来的值
        for(int i = start;i < nums.length;i++){
            path.add(nums[i]);
            res.add(new ArrayList<>(path));
            dfs(nums,i + 1);
            path.remove(path.size() - 1);
        }
    }
}

posted @ 2022-02-07 17:06  蹇爱黄  阅读(32)  评论(0)    收藏  举报