39. 组合总和 Combination Sum - LeetCode
可以重复使用元素,没有重复的元素
确定参数和返回值
确定终止条件
单层递归逻辑
二维 res
一维 path//单一结果,收集路径的过程
void backtracking(candidate, target, sum, startIndex)
if (sum > target) return;
if (sum == target)
res.add(path);
return;
for (int i = startIndex; i < candidate.length; i++)
path.add(cadidate[i]);
sum += candidate[i];
backtracking(candidate, target, sum, i);
sum -= candidate[i];
path.remove(path.size() - 1);
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backtracking(res, path, candidates, target, 0, 0);
return res;
}
private void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int startIndex) {
if (sum > target) return;
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = startIndex ; i < candidates.length; i++) {
path.add(candidates[i]);
sum += candidates[i];
backtracking(res, path, candidates, target, sum, i);
sum -= candidates[i];
path.remove(path.size() - 1);
}
}
}
40.组合总和II Combination Sum II - LeetCode
树层去重
树枝去重
二维 res
一维 path
Arrays.sort(nums);//一定要记得sort数组
void backtracking(nums, targetSum, sum, startIndex, used)
if (sum > targetSum) return;
if (sum == targetSum)
res.add(path);
return;
for (int i = startIndex; i < nums.length; i++)
if (i > 0 &&nums[i] == nums[i - 1] && used[i - 1] == 0) continue
path.add(nums[i]);
sum += nums[i];
used[i] = true;
backtracking(nums, targetSum, sum, i + 1, used);
path.remove(path.size() - 1);
sum -= nums[i];
used[i] = false;
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = new boolean[candidates.length];
Arrays.sort(candidates);
backtracking(res, path, candidates, target, used, 0, 0);
return res;
}
private void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, boolean[] used, int sum, int startIndex) {
if (sum > target) return;
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) continue;
path.add(candidates[i]);
sum += candidates[i];
used[i] = true;
backtracking(res, path, candidates, target, used, sum, i + 1);
path.remove(path.size() - 1);
sum -= candidates[i];
used[i] = false;
}
}
}
131.分割回文串Palindrome Partitioning - LeetCode
一维 path//切割方案
二维 res
void backtracking(String s, startIndex)
if (startIndex >= s.length())
res.add(path);
return;
for (int i = startIndex; i < s.length(); i++)
isP(s, startIndex + i)
path.add(子串)
else continue
backtracking(s, i + 1);
path.remove(子串);
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
List<String> path = new ArrayList<>();
backtracking(res, path, s, 0);
return res;
}
private void backtracking(List<List<String>> res, List<String> path, String s, int startIndex) {
if (startIndex >= s.length()) {
res.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isP(s, startIndex, i)) {
String str = s.substring(startIndex, i + 1);
path.add(str);
} else continue;
backtracking(res, path, s, i + 1);
path.remove(path.size() - 1);
}
}
private boolean isP(String s, int startIndex, int end) {
for (int i = startIndex, j = end; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}