当前位置: 首页 > news >正文

算法-回溯算法-组合问题

77. 组合https://leetcode.cn/problems/combinations/

给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。

你可以按 任何顺序 返回答案。

示例 1:

输入:n = 4, k = 2
输出:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],
]

示例 2:

输入:n = 1, k = 1
输出:[[1]]
class Solution {List<List<Integer>> ans=new ArrayList<>();List<Integer> path=new ArrayList<>();public List<List<Integer>> combine(int n, int k) {backtracking(n,k,1);return ans;}//start来记录下⼀层递归,搜索的起始位置。private void backtracking(int n,int k,int start){//遍历终止条件if(path.size()==k){ans.add(new ArrayList<>(path));return;}/**已经选择的元素path.size()还需要选择的元素k-path.size()*/for(int i=start;i<=n-(k-path.size())+1;i++){path.add(i);backtracking(n,k,i+1);path.remove(path.size()-1);}}
}

216. 组合总和 IIIhttps://leetcode.cn/problems/combination-sum-iii/description/

找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:

  • 只使用数字1到9
  • 每个数字 最多使用一次 

返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。

示例 1:

输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。

 示例 2:

输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。

 

class Solution {List<List<Integer>> ans = new ArrayList<>();List<Integer> path = new ArrayList<>();int sum =0;public List<List<Integer>> combinationSum3(int k, int n) {backtracking(k,n,1);return ans;}start来记录下⼀层递归,搜索的起始位置。private void backtracking(int k,int n,int start){//终止条件;组合大小=k或者和大于nif(path.size()==k||sum>n){if(sum==n){ans.add(new ArrayList<>(path));}return;}for(int i=start;i<=9-(k-path.size())+1;i++){path.add(i);sum+=i;backtracking(k,n,i+1);sum-=i;path.remove(path.size()-1);}}
}

 39. 组合总和https://leetcode.cn/problems/combination-sum/description/

给你一个 无重复元素 的整数数组 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]]
class Solution {List<List<Integer>> ans = new ArrayList<>();List<Integer> path = new ArrayList<>();int sum=0;public List<List<Integer>> combinationSum(int[] candidates, int target) {//排序是为了剪枝,如果加上当前元素已经大于target,那么就没有必要继续遍历下去Arrays.sort(candidates);backTracking(candidates, target, 0);return ans;}//start记录每层遍历的起始位置,元素可以无限制重复使用,因此start不需要变更private void backTracking(int[] candidates, int target, int start) {if (sum == target) {ans.add(new ArrayList<>(path));return;}for (int i = start; i < candidates.length; i++) {//当前元素大于target终止遍历if(candidates[i]>target){break;}//元素之和大于target终止遍历if(sum+candidates[i]>target){continue;}sum += candidates[i];path.add(candidates[i]);backTracking(candidates, target, i);path.remove(path.size()-1);sum -= candidates[i];}}
}

40. 组合总和 IIhttps://leetcode.cn/problems/combination-sum-ii/description/

给定一个候选人编号的集合 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]
]
class Solution {List<List<Integer>> ans = new ArrayList<>();List<Integer> path = new ArrayList<>();int sum=0;public List<List<Integer>> combinationSum2(int[] candidates, int target) {//记录每个元素使用boolean[] used = new boolean[candidates.length];Arrays.sort(candidates);backTracking(candidates, target,used, 0);return ans;}private void backTracking(int[] candidates, int target,boolean[] used,int start) {if (sum == target) {ans.add(new ArrayList<>(path));return;}for (int i = start; i < candidates.length && sum + candidates[i] <= target; i++) {//同层相同元素已经使用过则跳过if (i > 0 && candidates[i - 1] == candidates[i] && used[i - 1]==false) {continue;}used[i] = true;sum += candidates[i];path.add(candidates[i]);backTracking( candidates, target,used,i+1);path.remove(path.size()-1);sum -= candidates[i];used[i] = false;}}
}

http://www.lryc.cn/news/9513.html

相关文章:

  • ABAP中的Null值与space 以及 BW中ADSO的Key值
  • JavaScript库之Lodash常用方法
  • Kotlin新手教程二(Kotlin基本数据类型及基础语法)
  • git idea创建新分支,获取/合并主支代码的2个方法
  • CF1714A Everyone Loves to Sleep 题解
  • oracle官方下载历史版本JDK版本
  • 双击-jar包无法运行解决方法
  • 程序员的自我修养第七章——动态链接 (下)
  • 蓝桥杯刷题——基础篇(二)
  • PTA L1-049 天梯赛座位分配(详解)
  • Linux部分参数作用讲解
  • Java kafka
  • DBA之路---Stream数据共享同步机制与配置方法
  • CF1790E Vlad and a Pair of Numbers 题解
  • 漏洞预警|Apache Kafka Connect JNDI注入漏洞
  • 企业小程序开发步骤【教你创建小程序】
  • 刚性电路板的特点及与柔性电路板的区别
  • 扫码过磅+车牌识别,内蒙古蒙维过磅实现信息化管理
  • 蒙特卡洛计算圆周率
  • 生物信息场景下的用户需求
  • linux su(switch user)和sudo(superuser do)的区别?(sudo su与su的区别)
  • PostgreSQL的学习心得和知识总结(一百二十三)|深入理解PostgreSQL数据库开源扩展pg_dirtyread的使用场景和实现原理
  • ubuntu清理挖矿病毒
  • 【代码随想录训练营】【Day16】第六章|二叉树|104.二叉树的最大深度|559.n叉树的最大深度|111.二叉树的最小深度|222.完全二叉树的节点个数
  • transformer总结
  • dart flutter入门教程,开发手册 分享
  • 教育舆情监测关键词有哪些,TOOM教育舆情监测系统流程?
  • MySQL高级(一)
  • 如何将Python项目部署到新电脑上运行?
  • JVM和JAVA体系结构