资讯专栏INFORMATION COLUMN

【LC总结】回溯 (Subsets I II/Permutation I II/Combinatio

tuomao / 3379人阅读

摘要:不同数包含重复数为的时候,表示在外层的循环正在被使用,所以当前循环遇到为一定要跳过。对当前循环要添加的数组,在添加当前元素后进行递归,递归之后要将当前元素的使用标记改为,表示已经使用和递归完毕,然后再将这个元素从的末位删除。

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> subsets(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, res, new ArrayList(), 0);
        return res;
    }
    public void helper(int[] nums, List> res, List cur, int start) {
        res.add(new ArrayList(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> subsetsWithDup(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, new ArrayList(), res, 0);
        return res;
    }
    public void helper(int[] nums, List cur, List> res, int start) {
        res.add(new ArrayList(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> permute(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        helper(nums, new ArrayList(), res);
        return res;
    }
    public void helper(int[] nums, List cur, List> res) {
        if (cur.size() == nums.length) res.add(new ArrayList(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> permuteUnique(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, new ArrayList(), res, new boolean[nums.length]);
        return res;
    }
    public void helper(int[] nums, List cur, List> res, boolean[] used) {
        if (cur.size() == nums.length) {
            res.add(new ArrayList (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> combinationSum(int[] A, int target) {
        List> 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 cur, List> 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> combinationSum2(int[] A, int target) {
        List> 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 cur, List> res, int target, int start) {
        if (target == 0) {
            res.add(new ArrayList (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> combinationSum3(int k, int n) {
        List> res = new ArrayList<>();
        helper(k, n, new ArrayList<>(), res, 1);
        return res;
    }
    public void helper(int k, int n, List cur, List> 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);
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66091.html

相关文章

  • LeetCode 关于回溯问题的看法

    摘要:回溯算法在算法过程中就是类似于枚举算法,尝试在搜索过程中找到问题的解。 回溯算法( BackTrack )在算法过程中就是类似于枚举算法,尝试在搜索过程中找到问题的解。 使用回溯算法解题的一般步骤 使用回溯算法解题的一般步骤: 针对所给问题得出一般的解空间 用回溯搜索方法搜索解空间 使用深度优先去搜索所有解并包含适当的剪枝操作 LeetCode 使用回溯算法的题目主要有 36 题,...

    ASCH 评论0 收藏0
  • 【Leetcode】78. 子集

    摘要:题目给定一组不含重复元素的整数数组,返回该数组所有可能的子集幂集。说明解集不能包含重复的子集。示例输入输出题解全排列,部分排列这些问题都是回溯的题目。这个题目每个状态都是解,包括空也是解,所以直接都加进去就好。 题目 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ ...

    laznrbfe 评论0 收藏0
  • 【Leetcode】78. 子集

    摘要:题目给定一组不含重复元素的整数数组,返回该数组所有可能的子集幂集。说明解集不能包含重复的子集。示例输入输出题解全排列,部分排列这些问题都是回溯的题目。这个题目每个状态都是解,包括空也是解,所以直接都加进去就好。 题目 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ ...

    miqt 评论0 收藏0
  • Subsets 系列 Leetcode解题记录

    摘要:写这个系列是因为纪念一下去年的今天,就是年的月号,刷题第一天,今天是一周年纪念日。排除,就是返回一空的。复杂度分析算法课讲过,这个复杂度是指数次,能实现出来就行了,没法优化。复杂度分析不分析了,反正指数次。 Subsets 写这个系列是因为纪念一下去年的今天,就是2015年的9月14号,刷题第一天,今天是一周年纪念日。当时只敢做easy还得抄答案的我想啥时候能做上medium啊,事到如...

    gityuan 评论0 收藏0
  • [LintCode/LeetCode] Subsets & Subsets II

    Subsets Problem Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. Example ...

    tracy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<