LeetCode —— 回溯

77. 组合

给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。

示例:输入:n = 4, k = 2         输出: [ [1,2], [1,3], [1,4], [2,3], [2,4], [3,4]]

class Solution {
    List> list = new ArrayList<>();
    LinkedList path = new LinkedList<>();

    public List> combine(int n, int k) {
        backTrack(n, k, 1);
        return list;
    }

    public void backTrack(int n, int k, int startIndex){
        if(path.size() == k){
            list.add(new ArrayList<>(path));
            return;
        }
        for(int i = startIndex; i <= n; i++){
            path.add(i);
            backTrack(n, k, i + 1);
            path.removeLast();
        }
    }
}

17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

LeetCode —— 回溯_第1张图片

示例:输入:digits = "23"         输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

class Solution {
    List list = new ArrayList<>();
    StringBuilder item = new StringBuilder();

    public List letterCombinations(String digits) {
        if(digits.length() == 0){
            return list;
        }
        String[] numberString = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backTrack(digits, numberString, 0);
        return list;
    }

    public void backTrack(String digits, String[] numberString, int index){
        if(item.length() == digits.length()){
            list.add(item.toString());
            return;
        }

        String cur = numberString[digits.charAt(index) - '0'];
        for(int i = 0; i < cur.length(); i++){
            item.append(cur.charAt(i));
            backTrack(digits, numberString, index + 1);
            item.deleteCharAt(item.length() - 1);
        }
    }
}

216. 组合总和III

找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:(1)只使用数字1到9;(2)每个数字 最多使用一次 。返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。

示例:输入: k = 3, n = 9         输出: [[1,2,6], [1,3,5], [2,3,4]]

class Solution {
    List> list = new ArrayList<>();
    LinkedList path = new LinkedList<>();

    public List> combinationSum3(int k, int n) {
        backTrack(k, n, 1, 0);
        return list;
    }

    public void backTrack(int k, int targetSum, int startIndex, int sum){
        if(sum > targetSum || path.size() > k){
            return;
        }
        if(path.size() == k && sum == targetSum){
            list.add(new ArrayList<>(path));
            return;
        }

        for(int i = startIndex; i <= 9; i++){
            path.add(i);
            sum += i;
            backTrack(k, targetSum, i + 1, sum);
            path.removeLast();
            sum -= i;
        }
    }
}

39.  组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 

示例:输入:candidates = [2,3,6,7], target = 7         输出:[[2,2,3],[7]]

class Solution {
    List> list = new ArrayList<>();
    LinkedList path = new LinkedList<>();

    public List> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTrack(candidates, target, 0, 0);
        return list;
    }

    public void backTrack(int[] candidates, int target, int index, int sum){
        if(sum == target){
            list.add(new ArrayList<>(path));
            return;
        }

        for(int i = index; i < candidates.length; i++){
            if(sum + candidates[i] > target){
                break;
            }
            path.add(candidates[i]);
            sum += candidates[i];
            backTrack(candidates, target, i, sum);
            sum -= candidates[i];
            path.removeLast();
        }
    }
}

40. 组合总和II

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用 一次 。

示例:输入: candidates = [10,1,2,7,6,1,5], target = 8       输出: [ [1,1,6], [1,2,5], [1,7], [2,6] ]

class Solution {
    List> list = new ArrayList<>();
    LinkedList path = new LinkedList<>();

    public List> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTrack(candidates, target, 0, 0);
        return list;
    }

    public void backTrack(int[] candidates, int target, int startIndex, int sum){
        if(sum == target){
            list.add(new ArrayList<>(path));
            return;
        }

        for(int i = startIndex; i < candidates.length; i++){
            if(sum + candidates[i] > target){
                break;
            }
            // 跳过同一层使用过的元素
            if(i > startIndex && candidates[i] == candidates[i - 1]){
                continue;
            }
            path.add(candidates[i]);
            sum += candidates[i];
            backTrack(candidates, target, i + 1, sum);
            sum -= candidates[i];
            path.removeLast();
        }
    }
}

你可能感兴趣的:(leetcode)