其他分享
首页 > 其他分享> > 78. Subsets

78. Subsets

作者:互联网

如果你碰到排列组合的题,就想想是否可以用backtracking做,然后把模版记住:

    private List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backtracking(nums, 0, new ArrayList<>(), res);
        return res;
    }
    
    private void backtracking(int[] nums, int index, List<Integer> list, List<List<Integer>> res){
        res.add(new ArrayList(list));
        for(int i=index;i<nums.length;i++){
            list.add(nums[i]);
            backtracking(nums, i+1, list, res);
            list.remove(list.size()-1);
        }
    }

 

标签:Subsets,nums,int,res,List,new,backtracking,78
来源: https://www.cnblogs.com/feiflytech/p/15862693.html