Day27
组合总和
题目
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7 输出:[[2,2,3],[7]] 解释: 2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。 7 也是一个候选, 7 = 7 。 仅有这两种组合。 示例 2:
输入: candidates = [2,3,5], target = 8 输出: [[2,2,2,2],[2,3,3],[3,5]] 示例 3:
输入: candidates = [2], target = 1 输出: []
思路
- 同一个 数字可以 无限制重复被选取
代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> paths = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); // 将数组从小到大排列
backtracking(candidates, target, 0);
return res;
}
private void backtracking(int[] candidates, int target, int index){
if(target < 0) return;
if(target == 0) {
res.add(new ArrayList<>(paths));
return;
}
for(int i = index; i < candidates.length; i++){
paths.add(candidates[i]);
backtracking(candidates, target - candidates[i], i); // 将 i 传入下层是因为元素可重复使用
paths.remove(paths.size() - 1);
}
}
}
组合总和 II
题目
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8, 输出: [ [1,1,6], [1,2,5], [1,7], [2,6] ] 示例 2:
输入: candidates = [2,5,2,1,2], target = 5, 输出: [ [1,2,2], [5] ]
提示:
1 <= candidates.length <= 100 1 <= candidates[i] <= 50 1 <= target <= 30
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/combination-sum-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
本题的难点在于区别2中:集合(数组candidates)有重复元素,但还不能有重复的组合
回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili
代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> paths = new ArrayList<>();
boolean used[] = null;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates); // 将数组从小到大排列
used = new boolean[candidates.length];
Arrays.fill(used, false);
backtracking(candidates, target, 0);
return res;
}
private void backtracking(int[] candidates, int target, int index){
if(target < 0) return;
if(target == 0) {
res.add(new ArrayList<>(paths));
return;
}
for(int i = index; i < candidates.length; i++){
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 要对同一树层使用过的元素进行跳过
if(i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]){
continue;
}
used[i] = true;
paths.add(candidates[i]);
backtracking(candidates, target - candidates[i], i + 1); // 将 i +1 传入下层是因为元素不可重复使用
paths.remove(paths.size() - 1);
used[i] = false;
}
}
}
分割回文串
题目
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例: 输入: “aab” 输出: [ [“aa”,“b”], [“a”,“a”,“b”] ]
思路
本题这涉及到两个关键问题:
- 切割问题
- 判断回文
回溯三部曲
- 递归函数参数
全局变量数组path存放切割后回文的子串,二维数组result存放结果集。 (这两个参数可以放到函数参数里)
本题递归函数参数还需要startIndex,因为切割过的地方,不能重复切割,和组合问题也是保持一致的。
List<List<String>> res = new ArrayList<>();
List<String> paths = new ArrayList<>();
public void backTracking(String s, int index)
- 递归函数终止条件
// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
if(index >= s.length()) {
res.add(new ArrayList<>(paths));
return;
}
- 单层搜索的逻辑
在for (int i = startIndex; i < s.size(); i++)
循环中,我们 定义了起始位置startIndex,那么 [startIndex, i] 就是要截取的子串。
首先判断这个子串是不是回文,如果是回文,就加入在path
中,path用来记录切割过的回文子串。
for(int i = index; i < s.length(); i++){
if(!isPalindrome(s, index, i)) {
continue;
}
paths.add(s.substring(index, i + 1));
backTracking(s, i + 1); //起始位置后移,保证不重复
paths.remove(paths.size() - 1);
}
注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1。
判断回文子串
private boolean isPalindrome(String s, int left, int right) {
while(left < right){
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
代码
class Solution {
List<List<String>> res = new ArrayList<>();
List<String> paths = new ArrayList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0);
return res;
}
public void backTracking(String s, int index){
if(index >= s.length()) {
res.add(new ArrayList<>(paths));
return;
}
for(int i = index; i < s.length(); i++){
if(!isPalindrome(s, index, i)) {
continue;
}
paths.add(s.substring(index, i + 1));
backTracking(s, i + 1); //起始位置后移,保证不重复
paths.remove(paths.size() - 1);
}
}
//判断是否是回文串
private boolean isPalindrome(String s, int left, int right) {
while(left < right){
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}