216.组合总和III
题目链接:https://leetcode.com/problems/combination-sum-iii
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
说明:所有数字都是正整数。
解集不能包含重复的组合。
示例 1: 输入: k = 3, n = 7 输出: [[1,2,4]]
示例 2: 输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]]
思路:
两层for循环?
- 返回条件:叶子节点,已选k个元素,和为n
- 单层搜索:集合固定的就是9个数[1,…,9],for循环固定i<=9
回溯
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum3(int k, int n) {
backtracking(k, n, 1);
return result;
}
private void backtracking(int k, int n, int startIndex) {
if (path.size() == k && sum != n) { // 剪枝自定义
return;
}
if (path.size() == k && sum == n) {
result.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= 9; i++) {
path.add(i);
sum += i;
backtracking(k, n, i+1);
path.remove(path.size()-1);
sum -= i;
}
}
}
回溯+剪枝
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum3(int k, int n) {
backtracking(k, n, 1);
return result;
}
private void backtracking(int k, int n, int startIndex) {
if (sum > n) { // 限制和值
return;
}
//if (path.size() > k) { // 限制元素个数,留够足够多元素给剩下所需,for循环至多从哪里开始
// return;
//}
if (path.size() == k && sum == n) {
result.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= 9 -(k-path.size())+1; i++) {
path.add(i);
sum += i;
backtracking(k, n, i+1);
path.remove(path.size()-1);
sum -= i;
}
}
}
时间复杂度: O(n * 2^n)
空间复杂度: O(n)
17.电话号码的字母组合
题目链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Input: digits = “23”
Output: [“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
思路:
- 数字和字母的映射,map二维数组,注意0和1
- 回溯法树形结构解决多层for循环
- 确定回溯函数参数:index是记录遍历digits第几个数字了,树的深度
- 确定终止条件:index 等于 输入的数字个数,“23” index= 2必须遍历完成
- 确定单层遍历逻辑:用index找到指向的数字以及对应的字符集
- 注意:输入1 * #按键等等异常情况???
回溯法
class Solution {
StringBuilder sb = new StringBuilder();
List<String> result = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if (digits.length() == 0) return result;
String[] map = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
backtracking(digits, map, 0);
return result;
}
private void backtracking(String digits, String[] map, int index){
if (index == digits.length()) {
result.add(sb.toString());
return;
}
// 将index指向的数字转为int
int digit = digits.charAt(index) - '0'; // if "23", then 0->2, 1->3
String letters = map[digit];
for (int i = 0; i < letters.length(); i++) {
sb.append(letters.charAt(i));
backtracking(digits, map, index+1); // index+1,下一个数字
sb.deleteCharAt(sb.length()-1);
}
}
}
时间复杂度: O(3^m * 4^n),其中 m 是对应四个字母的数字个数,n 是对应三个字母的数字个数
空间复杂度: O(3^m * 4^n)
???