day27 回溯
组合总数
题目链接: 组合总数
题目描述
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
解答
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;
}
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;
path.add(candidates[i]);
backTracking(res,path,candidates,target , sum+candidates[i] , i);
path.remove(path.size() - 1);
}
}
}
组合总数
题目链接: 组合总数
题目描述
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
解答
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;
}
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 += candidates[i];
path.add(candidates[i]);
backTracking(candidates,target,i + 1);
used[i] = false;
sum -= candidates[i];
path.removeLast();
}
}
}
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> ans = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTracking(candidates , target , 0);
return ans;
}
void backTracking(int[] candidates , int target , int startIndex) {
if (sum == target) {
ans.add(new ArrayList<>(path));
return;
}
for (int i = startIndex ; i < candidates.length && sum + candidates[i] <= target ; i++) {
if ( i > startIndex && candidates[i] == candidates[i -1]) {
continue;
}
sum += candidates[i];
path.add(candidates[i]);
backTracking(candidates , target , i + 1);
int temp = path.getLast();
sum -= temp;
path.removeLast();
}
}
}
分割回文串
题目链接: 分割回文串
题目描述
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
解题
class Solution {
List<List<String>> lists = new ArrayList<>();
Deque<String> deque = new LinkedList<>();
public List<List<String>> partition(String s) {
backTracking(s , 0);
return lists;
}
void backTracking(String s , int index) {
if (index >= s.length()) {
lists.add(new ArrayList(deque));
return;
}
for (int i = index ; i < s.length() ; i++) {
if (isPalindrome(s , index , i)) {
String temp = s.substring(index , i + 1);
deque.addLast(temp);
} else {
continue;
}
backTracking( s , i +1);
deque.removeLast();
}
}
boolean isPalindrome(String s , int startIndex , int endIndex) {
for (int i = startIndex , j = endIndex ; i < j ; i++ , j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}