78. Subsets

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        backtracking(nums, 0, new ArrayList<>());
        return res;
    }

    private void backtracking(int[] nums, int index, List<Integer> list) {
        res.add(new ArrayList(list));
        for (int i = index; i < nums.length; i++) {
            list.add(nums[i]);
            backtracking(nums, i + 1, list);  //here, the "i+1" cannot be replaced with "index+1"
            list.remove(list.size() - 1);
        }
    }

90. Subsets II

package backtracking;

import java.util.*;

public class SubsetsII90 {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        Set<List<Integer>> set = new HashSet<>();
        backtracking(nums, 0, new ArrayList<>(), set);
        return res;
    }

    private void backtracking(int[] nums, int index, List<Integer> temp, Set<List<Integer>> set) {
        res.add(new ArrayList(temp));
        for (int i = index; i < nums.length; i++) {  
            if (i > index && nums[i] == nums[i - 1])  //here, i > index, instead of i > 0
                continue;
            temp.add(nums[i]);
            backtracking(nums, i + 1, temp, set);
            temp.remove(temp.size() - 1);
        }
    }
}

46. Permutations

    private List<List<Integer>> res = new ArrayList<>();
    boolean[] visited;

    public List<List<Integer>> permute(int[] nums) {
        visited = new boolean[nums.length];
        backtracking(nums, 0, new ArrayList<>());
        return res;
    }

    private void backtracking(int[] nums, int index, List<Integer> temp) {
        if (temp.size() == nums.length) {
            res.add(new ArrayList(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i])
                continue;
            temp.add(nums[i]);
            visited[i] = true;
            backtracking(nums, i + 1, temp);
            temp.remove(temp.size() - 1);
            visited[i] = false;
        }
    }

47. Permutations II

    List<List<Integer>> res = new ArrayList<>();

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

    private void backtracking(int[] nums, int index, List<Integer> temp, boolean[] visited, List<List<Integer>> res) {
        if (temp.size() == nums.length) {
            res.add(new ArrayList(temp));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (visited[i])
                continue;
            if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) //here !visited[i - 1] is important
                continue;
            temp.add(nums[i]);
            visited[i] = true;
            backtracking(nums, i + 1, temp, visited, res);
            temp.remove(temp.size() - 1);
            visited[i] = false;
        }
    }

39. Combination Sum

    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates, target, 0, new ArrayList<>());
        return res;
    }

    private void backTracking(int[] candidates, int target, int index, List<Integer> temp) {
        if (target == 0) {
            res.add(new ArrayList(temp));
            return;
        }
        if (target < 0)
            return;
        for (int i = index; i < candidates.length; i++) {
            temp.add(candidates[i]);
            backTracking(candidates, target - candidates[i], i, temp);  //Because we can reuse the candidates, so next index should be i.
            temp.remove(temp.size() - 1);
        }
    }

40. Combination Sum II

    private List<List<Integer>> res = new ArrayList<>();
    private boolean[] visited;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        visited = new boolean[candidates.length];
        Arrays.sort(candidates);
        backTracking(candidates, target, 0, new ArrayList<>());
        return res;
    }

    private void backTracking(int[] candidates, int target, int index, List<Integer> temp) {
        if (target == 0) {
            res.add(new ArrayList(temp));
            return;
        } else if (target < 0)
            return;
        for (int i = index; i < candidates.length; i++) {
            if (i > 0 && candidates[i] == candidates[i - 1] && !visited[i - 1])
                continue;
            temp.add(candidates[i]);
            visited[i] = true;
            backTracking(candidates, target - candidates[i], i + 1, temp);
            temp.remove(temp.size() - 1);
            visited[i] = false;
        }
    }

216. Combination Sum III

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        backTracking(k, n, 1, new ArrayList<>());
        return res;
    }

    private void backTracking(int k, int n, int index, List<Integer> temp) {
        if (k == 0 && n == 0) {
            res.add(new ArrayList(temp));
            return;
        } else if (n < 0 || k < 0)
            return;
        for (int i = index; i <= 9; i++) {
            temp.add(i);
            backTracking(k - 1, n - i, i + 1, temp);
            temp.remove(temp.size() - 1);
        }
    }

 

posted on 2021-12-31 11:31  阳光明媚的菲越  阅读(22)  评论(0编辑  收藏  举报