如果你碰到排列组合的题,就想想是否可以用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);
        }
    }

 

posted on 2022-02-04 12:37  阳光明媚的菲越  阅读(27)  评论(0编辑  收藏  举报