leetcode 77. 组合
题目描述
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
示例
输入:n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
题目链接
https://leetcode.cn/problems/combinations
解决思路
回溯,用 startIndex表示访问的起始下标。因为要 k个数, path.size()为已经取的个数,所以 k - path.size()为还需要取的个数。所以下标一定要满足小于等于 n - (k -  path.size()) + 1,才能保证能取到 k个数
class Solution {
    List<List<Integer>> paths = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        traceBack(n, k, 1);
        return paths;
    }
    public void traceBack(int n, int k, int startIndex) {
        if (path.size() == k) {
            paths.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            traceBack(n, k, i + 1);
            path.removeLast();
        }
    }
}
leetcode 216. 组合总和 III
题目描述
找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
- 只使用数字1到9
 - 每个数字 最多使用一次
 
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
示例
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。
题目链接
https://leetcode.cn/problems/combination-sum-iii
解决思路
还是回溯,但是终止条件为 list.size() == k只要满足就返回,在这基础上,如果满足 sum == n,则将路径添加到结果集合里
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        traceBack(k, n, 0, 1);
        return res;
    }
    public void traceBack(int k , int n, int sum, int startIndex) {
        if (list.size() == k) {
            if (sum == n) {
                res.add(new ArrayList<>(list));
            }
            return;
        }
        for (int i = startIndex; i <= 9; i++) {
            list.add(i);
            traceBack(k, n, sum + i, i + 1);
            list.removeLast();
        }
    }
}
leetcode 17. 电话号码的字母组合
题目描述
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
题目链接
https://leetcode.cn/problems/letter-combinations-of-a-phone-number
解决思路
构建一个 map映射 <number, character>。数字顺序不变。所以 index定位当前的数字下标。每次通过获取数字映射的字母,进行回溯。
class Solution {
    List<String> res = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    Map<Character, String> hm = new HashMap<>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
    public List<String> letterCombinations(String digits) {
        traceBack(digits, 0);
        return res;
    }
    public void traceBack(String digits, int index) {
        if (sb.length() == digits.length()) {
            res.add(sb.toString());
            return;
        }
        String str = hm.get(digits.charAt(index));
        for (Character ch : str.toCharArray()) {
            sb.append(ch);
            traceBack(digits, startIndex + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
                
            
评论