1 组合问题
组合(medium难度)
本题方法和代码来源:
作者:carlsun-2
链接:https://leetcode-cn.com/problems/combinations/solution/77-zu-he-hui-su-fa-jing-dian-ti-mu-by-carlsun-2/
来源:力扣(LeetCode)
(本人将原文的C++代码翻译成Java代码)
// 存放符合条件结果的集合
LinkedList<List<Integer>> result = new LinkedList<>();
// ⽤来存放符合条件结果
LinkedList<Integer> path = new LinkedList<>();
// 存放符合条件结果的集合
LinkedList<List<Integer>> result = new LinkedList<>();
// ⽤来存放符合条件结果
LinkedList<Integer> path = new LinkedList<>();
void backTracking(int n,int k, int startIndex)
if(path.size()==k){
result.add(new LinkedList<>(path));
return;
}
for(int i = startIndex; i <= n ;i++){
path.add(i);// 处理节点
backTracking(n,k,i+1);
path.removeLast();// 回溯,撤销处理的节点
}
完整代码如下:
class Solution {
// 存放符合条件结果的集合
LinkedList<List<Integer>> result = new LinkedList<>();
// ⽤来存放符合条件结果
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
backTracking(n,k,1);
return result;
}
private void backTracking(int n,int k, int startIndex){
if(path.size()==k){
result.add(new LinkedList<>(path));
return;
}
for(int i = startIndex; i <= n ;i++){
path.add(i);// 处理节点
backTracking(n,k,i+1);// 递归:控制树的纵向遍历,注意下一层搜索要从i+1开始
path.removeLast();// 回溯,撤销处理的节点
}
}
}
for(int i = startIndex; i <= n ;i++){
path.add(i);
backTracking(n,k,i+1);
path.removeLast();
}
for(int i = startIndex; i <= n ;i++)
for(int i = startIndex; i <= n - (k - path.size()) + 1;i++)
优化后整体代码如下:
class Solution {
// 存放符合条件结果的集合
LinkedList<List<Integer>> result = new LinkedList<>();
// ⽤来存放符合条件结果
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
backTracking(n,k,1);
return result;
}
private void backTracking(int n,int k, int startIndex){
if(path.size()==k){
result.add(new LinkedList<>(path));
return;
}
for(int i = startIndex; i <= n - (k - path.size()) + 1;i++){
path.add(i);// 处理节点
backTracking(n,k,i+1);// 递归:控制树的纵向遍历,注意下一层搜索要从i+1开始
path.removeLast();// 回溯,撤销处理的节点
}
}
}
本题方法和代码来源:
作者:sdwwld
链接:https://leetcode-cn.com/problems/combinations/solution/hui-su-jie-jue-tu-wen-xiang-jie-by-sdwwld/
来源:力扣(LeetCode)
n叉树的遍历
private void backtrack() {
if ("终止条件") {
return;
}
for (int i = ?; i <= n - k + 1; i++) {
//逻辑运算1,(可有可无,视情况而定)
//递归调用,遍历每一个分支
backtrack(list, n, k - 1, i + 1, tempList);
//逻辑运算2,(可有可无,视情况而定)
}
}
作者:sdwwld
链接:https://leetcode-cn.com/problems/combinations/solution/hui-su-jie-jue-tu-wen-xiang-jie-by-sdwwld/
来源:力扣(LeetCode)
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> list = new LinkedList<>();
backtrack(list, n, k, 1, new ArrayList<>());
return list;
}
private void backtrack(List<List<Integer>> list, int n, int k, int start, List<Integer> tempList) {
//终止条件,找到一对组合
if (k == 0) {
list.add(new LinkedList<>(tempList));
return;
}
//注意这里的i不能从0开始,如果从0开始会出现重复的,比如[1,2]和[2,1]
for (int i = start; i <= n - k + 1; i++) {
//把当前值添加到集合中
tempList.add(i);
//递归调用,继续下一个分支
backtrack(list, n, k - 1, i + 1, tempList);
//从当前分支跳到下一个分支的时候要把之前添加的值给移除
tempList.remove(tempList.size() - 1);
}
}
作者:sdwwld
链接:https://leetcode-cn.com/problems/combinations/solution/hui-su-jie-jue-tu-wen-xiang-jie-by-sdwwld/
来源:力扣(LeetCode)
组合总和Ⅲ(medium难度)
https://leetcode-cn.com/problems/combination-sum-iii/
本方法思路和代码思路来源:
作者:carlsun-2
链接:https://leetcode-cn.com/problems/combination-sum-iii/solution/216-zu-he-zong-he-iiihui-su-fa-mo-ban-ti-by-carlsu/
来源:力扣(LeetCode)
(本人将原文的C++代码翻译成Java代码)
需要⼀维数组path来存放符合条件的结果,⼆维数组result来存放结果集。
这⾥我定义path 和 result为全局变量。⾄于为什么取名为path?从上⾯树形结构中,可以看出,结果其实就是⼀条根节点到叶⼦节点的路径。
// 存放结果集
List<List<Integer>> result = new LinkedList<>();
// 符合条件的结果
LinkedList<Integer> path = new LinkedList<>();
// 存放结果集
List<List<Integer>> result = new ArrayList<>();
// 符合条件的结果
List<Integer> path = new ArrayList<>();
private void backTrack(int targetSum, int k, int sum, int startIndex)
if (path.size() == k) {
if(sum == targetSum){
result.add(new ArrayList(path));
}
// 如果path.size() == k 但sum != targetSum 直接返回
return;
for (int i = startIndex; i <= 9; i++) {
sum += i;// 处理
path.add(i);// 处理
backTrack(targetSum, k, sum, i + 1);//注意i+1调整startIndex
sum -= i;// 回溯
path.remove(path.size() - 1);// 回溯
}
整体代码如下:
class Solution {
// 存放结果集
List<List<Integer>> result = new ArrayList<>();
// 符合条件的结果
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(n, k, 0, 1);
return result;
}
// targetSum:⽬标和,也就是题⽬中的n。
// k:题⽬中要求k个数的集合。
// sum:已经收集的元素的总和,也就是path⾥元素的总和。
// startIndex:下⼀层for循环搜索的起始位置。
private void backTrack(int targetSum, int k, int sum, int startIndex) {
if (path.size() == k) {
if (sum == targetSum) {
result.add(new ArrayList(path));
}
// 如果path.size() == k 但sum != targetSum 直接返回
return;
}
for (int i = startIndex; i <= 9; i++) {
sum += i;// 处理
path.add(i);// 处理
backTrack(targetSum, k, sum, i + 1);//注意i+1调整startIndex
sum -= i;// 回溯
path.remove(path.size() - 1);// 回溯
}
}
}
剪枝优化:
if (sum > targetSum) { // 剪枝操作
return; // 如果path.size() == k 但sum != targetSum 直接返回
}
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++)
优化以后的代码如下:
class Solution {
// 存放结果集
List<List<Integer>> result = new ArrayList<>();
// 符合条件的结果
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(n, k, 0, 1);
return result;
}
// targetSum:⽬标和,也就是题⽬中的n。
// k:题⽬中要求k个数的集合。
// sum:已经收集的元素的总和,也就是path⾥元素的总和。
// startIndex:下⼀层for循环搜索的起始位置。
private void backTrack(int targetSum, int k, int sum, int startIndex) {
if (sum > targetSum) { // 剪枝操作
return; // 如果path.size() == k 但sum != targetSum 直接返回
}
if (path.size() == k) {
if (sum == targetSum) {
result.add(new ArrayList(path));
}
// 如果path.size() == k 但sum != targetSum 直接返回
return;
}
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
sum += i;// 处理
path.add(i);// 处理
backTrack(targetSum, k, sum, i + 1);//注意i+1调整startIndex
sum -= i;// 回溯
path.remove(path.size() - 1);// 回溯
}
}
}
不用targetSum的代码如下:
class S