class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTracking(n, k, 1, 0);
return result;
}
private void backTracking(int targetSum, int k, int startIndex, int sum) {
// 减枝
if (sum > targetSum) {
return;
}
if (path.size() == k) {
if (sum == targetSum) result.add(new ArrayList<>(path));
return;
}
// 减枝 9 - (k - path.size()) + 1
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
path.add(i);
sum += i;
backTracking(targetSum, k, i + 1, sum);
//回溯
path.removeLast();
//回溯
sum -= i;
}
}
}
思路:
确定终止条件:k是限制树深的元素。如果k和path的长度已经相等,说明我们已经找到符合要求的一个list,并且如果这个list的sum等于我们的n也就是targetsum,那我们就可以把这个加入到result里面。
单层搜索逻辑:因为限制【1,9】所以for只能固定搜索1-9,处理过程就是选取一个i加入到path里,然后计算sum,递归下面就要有回溯的过程,和递归的逻辑是对应的。
两个剪枝操作:
1. 这里主要是对于i进行剪枝操作
9 - i + 1 = k - path.size()
-i = - 9 - 1 + k - path.size()
i = 9 - k + path.size() + 1
2. 对sum进行剪枝操作
sum > targetSum
class Solution {
List<String> result = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if(digits.length() == 0 || digits == null){
return result;
}
//定义一个map
String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backtracking(digits, numString, 0);
return result;
}
StringBuilder temp = new StringBuilder();
public void backtracking(String digits, String[] numString, int index){
if(index == digits.length()){
result.add(temp.toString());
return;
}
//digits.charAt(index) - '0'表示 变为数字, 然后str表示单个数字代表的字符串
String str = numString[digits.charAt(index) - '0'];
for(int i = 0; i < str.length(); i++){
temp.append(str.charAt(i));
backtracking(digits, numString, index + 1);
temp.deleteCharAt(temp.length() - 1);
}
}
}
思路:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
backtracking(res, new ArrayList<>(), candidates, target, 0,0);
return res;
}
public void backtracking(List<List<Integer>> res, List<Integer> path, int[]candidates, int target, int sum, int index){
if(sum == target){
res.add(new ArrayList<>(path));
return;
}
for (int i = index; i < candidates.length; i++){
if(sum + candidates[i] > target){
break;
}
sum = sum + candidates[i];
path.add(candidates[i]);
backtracking(res, path, candidates, target, sum, i);
sum = sum - candidates[i];
path.remove(path.size()-1);
}
}
}
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> ans = new ArrayList<>();
boolean[] used;
int sum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
used = new boolean[candidates.length];
Arrays.fill(used, false); // 为了帮助判断树层节点是否已经遍历
//为了方便树层去重,现将数组进行排序
Arrays.sort(candidates);
backtracking(candidates, target, 0);
return ans;
}
public void backtracking(int[] candidates, int target, int startIndex){
if( sum == target){
ans.add(new ArrayList(path));
}
for(int i = startIndex; i < candidates.length; i++){
if(sum + candidates[i] > target){
break;
}
//树层出现重复节点,同一层的第一个节点已经被访问过,所以直接跳过此节点
if(i > 0 && candidates[i] == candidates[i-1] && !used[i-1]){
continue;
}
used[i] = true;
sum = sum + candidates[i];
path.add(candidates[i]);
//每个节点只能选择一次,所以从下一位开始
backtracking(candidates, target, i + 1);
used[i] = false;
sum = sum - candidates[i];
path.removeLast();
}
}
}
思路:
主要在于去重,分为树根去重和树层去重,因为题目要求
同一树枝上的相同的两个元素可以选取,但是同一树层上的两个元素不可以重复选取。
如果candidates[i] == candidates[i - 1]
并且 used[i - 1] == false
,就说明:前一个树枝,使用了candidates[i - 1],也就是说同一树层使用过candidates[i - 1]。
这也就是为什么candidates数组需要提前排序好,为了相邻的元素进行去重操作