【LC总结】回溯 (Subsets I II/Permutation I II/Combination Sum I II)

Subsets

Problem

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solution

public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, res, new ArrayList<Integer>(), 0);
        return res;
    }
    public void helper(int[] nums, List<List<Integer>> res, List<Integer> cur, int start) {
        res.add(new ArrayList<Integer>(cur));
        for (int i = start; i < nums.length; i++) {
            cur.add(nums[i]);
            helper(nums, res, cur, i+1);
            cur.remove(cur.size()-1);
        }
    }
}

Subsets II

Problem

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

Solution

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, new ArrayList<Integer>(), res, 0);
        return res;
    }
    public void helper(int[] nums, List<Integer> cur, List<List<Integer>> res, int start) {
        res.add(new ArrayList<Integer>(cur));
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i-1]) continue;
            cur.add(nums[i]);
            helper(nums, cur, res, i+1);
            cur.remove(cur.size()-1);
        }
    }
}

Permutations (不同数)

Problem

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Solution

public class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        helper(nums, new ArrayList<Integer>(), res);
        return res;
    }
    public void helper(int[] nums, List<Integer> cur, List<List<Integer>> res) {
        if (cur.size() == nums.length) res.add(new ArrayList<Integer>(cur));
        else for (int i = 0; i < nums.length; i++) {
            if (cur.contains(nums[i])) continue;
            cur.add(nums[i]);
            helper(nums, cur, res);
            cur.remove(cur.size()-1);
        }
    }
}

Permutations II (包含重复数)

Problem

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

Note

used[i]为true的时候,表示在外层的循环正在被使用,所以当前循环遇到used[i]为true一定要跳过。
对当前循环要添加的数组cur,在添加当前元素后进行递归,递归之后要将当前元素nums[i]的使用标记used[i]改为false,表示已经使用和递归完毕,然后再将这个元素从cur的末位删除。

Solution

public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, new ArrayList<Integer>(), res, new boolean[nums.length]);
        return res;
    }
    public void helper(int[] nums, List<Integer> cur, List<List<Integer>> res, boolean[] used) {
        if (cur.size() == nums.length) {
            res.add(new ArrayList<Integer> (cur));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i] || (i != 0 && nums[i] == nums[i-1] && !used[i-1])) continue;
            else {    
                used[i] = true;
                cur.add(nums[i]);
                helper(nums, cur, res, used);
                used[i] = false;
                cur.remove(cur.size()-1);
            }
        }
    }
}

Combination Sum

Problem

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:

[
  [7],
  [2, 2, 3]
]

Solution

public class Solution {
    public List<List<Integer>> combinationSum(int[] A, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (A == null || A.length == 0) return res;
        Arrays.sort(A);
        helper(A, new ArrayList<Integer>(), res, target, 0);
        return res;
    }
    public void helper(int[] A, List<Integer> cur, List<List<Integer>> res, int target, int start) {
        if (target == 0) res.add(new ArrayList<>(cur));
        if (target < 0) return;
        for (int i = start; i < A.length; i++) {
            cur.add(A[i]);
            helper(A, cur, res, target-A[i], i);
            cur.remove(cur.size()-1);
        }
    }
}

Combination Sum II

Problem

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:

[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

Solution

public class Solution {
    public List<List<Integer>> combinationSum2(int[] A, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (A == null || A.length == 0) return res;
        Arrays.sort(A);
        helper(A, new ArrayList<>(), res, target, 0);
        return res;
    }
    public void helper(int[] A, List<Integer> cur, List<List<Integer>> res, int target, int start) {
        if (target == 0) {
            res.add(new ArrayList<Integer> (cur));
            return;
        }
        if (target < 0) return;
        for (int i = start; i < A.length; i++) {
            if (i != start && A[i] == A[i-1]) continue;
            cur.add(A[i]);
            helper(A, cur, res, target-A[i], i+1);
            cur.remove(cur.size()-1);
        }
    }
}

Combination Sum III

Problem

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

Solution

public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> res = new ArrayList<>();
        helper(k, n, new ArrayList<>(), res, 1);
        return res;
    }
    public void helper(int k, int n, List<Integer> cur, List<List<Integer>> res, int start) {
        if (n < 0) return;
        if (k == 0 && n == 0) res.add(new ArrayList<>(cur));
        for (int i = start; i <= 9; i++) {
            cur.add(i);
            helper(k-1, n-i, cur, res, i+1);
            cur.remove(cur.size()-1);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值