编程语言
首页 > 编程语言> > leetcode刷题----数组子集--回溯算法

leetcode刷题----数组子集--回溯算法

作者:互联网

78 


    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){
        for(Integer n: tempList){
            System.out.print(n + " ");
        }
        System.out.println();
        list.add(new ArrayList<>(tempList));
        for(int i = start; i < nums.length; i++){
            tempList.add(nums[i]);

            backtrack(list, tempList, nums, i + 1);
            tempList.remove(tempList.size() - 1);
        }
    }

90

    public List<List<Integer>> subsetsWithDup(int[] nums){
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);
        dfs(ans, new ArrayList<>(), nums, 0);
        return ans;
    }

    public void dfs(List<List<Integer>> set, List<Integer> tempList, int[] nums, int start){
        set.add(new ArrayList<>(tempList));
        for(int i = start; i < nums.length; i++){
            if(i > start && nums[i] == nums[i - 1]) continue;
            tempList.add(nums[i]);
            dfs(set, tempList, nums, i + 1);
            tempList.remove(tempList.size() - 1);
        }
    }

46

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        // Arrays.sort(nums); // not necessary
        backtrack(list, new ArrayList<>(), nums);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
        if(tempList.size() == nums.length){
            list.add(new ArrayList<>(tempList));
        } else{
            for(int i = 0; i < nums.length; i++){
                if(tempList.contains(nums[i])) continue; // element already exists, skip
                tempList.add(nums[i]);
                backtrack(list, tempList, nums);
                tempList.remove(tempList.size() - 1);
            }
        }
    }

47

    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){
        if(tempList.size() == nums.length){
            list.add(new ArrayList<>(tempList));
        } else{
            for(int i = 0; i < nums.length; i++){
                if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue;
                used[i] = true;
                tempList.add(nums[i]);
                backtrack(list, tempList, nums, used);
                used[i] = false;
                tempList.remove(tempList.size() - 1);
            }
        }
    }

 

39

    public List<List<Integer>> combinationSum(int[] candidate, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        backTrack(ans, new ArrayList<>(), candidate, target, 0, 0);
        return ans;
    }

    public void backTrack(List<List<Integer>> ans, List<Integer> tempList, int[] candidate, int target, int sum, int start) {
        if(sum > target)    return;
        if (sum == target) ans.add(new ArrayList<>(tempList));
        for (int i = start; i < candidate.length; i++) {
            tempList.add(candidate[i]);
            backTrack(ans, tempList, candidate, target, sum + candidate[i], i);
            tempList.remove(tempList.size() - 1);
        }
    }

 

40

 

马各阿牛 发布了66 篇原创文章 · 获赞 25 · 访问量 5万+ 私信 关注

标签:nums,int,list,List,回溯,new,leetcode,tempList,刷题
来源: https://blog.csdn.net/weianluo/article/details/104071019