Leetcode 组合总和问题
文章目录
- 39.组合总和
- 40.组合总和Ⅱ
- 组合总和Ⅲ
- 组合总和Ⅳ
39.组合总和
题目设定:
给的数组没有重复元素,同一个元素可以无限引用,最后的结果不能重复
树形图如下:
因为没有重复元素,不用考虑去重的问题
元素可以重复使用,所以递归的时候需要从 i 往下,而不是i+1
对一个数组且元素相关,需要startindex
故代码如下:
class Solution {private List<List<Integer>> ans = new ArrayList<List<Integer>>();private LinkedList<Integer> path = new LinkedList<Integer>(); public List<List<Integer>> combinationSum(int[] candidates, int target) {backtrack(candidates,target,0,0);return ans;}public void backtrack(int[] candidates,int target,int sum,int startIndex){if(sum>target) return;if(sum == target){ans.add(new ArrayList<> (path));return;}for(int i = startIndex;i<candidates.length;i++){sum+=candidates[i];path.add(candidates[i]);backtrack(candidates,target,sum,i);sum-=candidates[i];path.removeLast();}}
}
剪枝操作:
只需判断sum+candidates[i]<=target即可
但注意,剪枝需要先排序
剪枝后代码:
class Solution {private List<List<Integer>> ans = new ArrayList<List<Integer>>();private LinkedList<Integer> path = new LinkedList<Integer>(); public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);backtrack(candidates,target,0,0);return ans;}public void backtrack(int[] candidates,int target,int sum,int startIndex){if(sum>target) return;if(sum == target){ans.add(new ArrayList<> (path));return;}for(int i = startIndex;i<candidates.length && sum+candidates[i]<=target;i++){sum+=candidates[i];path.add(candidates[i]);backtrack(candidates,target,sum,i);sum-=candidates[i];path.removeLast();}}
}
40.组合总和Ⅱ
题目设定:
给的集合有重复元素,每个元素只能用一次,最后不能有重复集合
树形图:
注意:
有重复元素,就需要去重
去重方法:创建一个boolean[] used 数组,通过一下代码去重
if(i>0 && candidates[i]==candidates[i-1] && !used[i-1])continue;
只能用一次,递归的时候应该是 i+1 ,而不是 i
剪枝的操作同上
代码如下:
class Solution {private List<List<Integer>> ans = new ArrayList<List<Integer>>();private LinkedList<Integer> path = new LinkedList<Integer>();public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);boolean[] used = new boolean[candidates.length];backtrack(candidates,target,0,0,used);return ans;}public void backtrack(int[] candidates, int target,int startIndex,int sum,boolean[] used){if(sum>target) return;if(sum == target){ans.add(new ArrayList<>(path));return;}for(int i = startIndex;i<candidates.length && sum+candidates[i]<=target;i++){if(i>0 && candidates[i]==candidates[i-1] && !used[i-1])continue;used[i] = true;path.add(candidates[i]);sum += candidates[i];backtrack(candidates,target,i+1,sum,used);sum -= candidates[i];path.removeLast();used[i] = false;}}
}
组合总和Ⅲ
题目设定:
找到n个数 和为k 只能有1-9数组且不能有重复数字
树形图:
没有重复数字,只有1-9,即不用去重
数字只能有一个,递归则为 i+1
即先要找k个数字,再判断其和,如果为target,加入ans,如果不是,两种情况一起return到上一层
class Solution {private List<List<Integer>> ans = new ArrayList<List<Integer>>();private LinkedList<Integer> path = new LinkedList<Integer>();public List<List<Integer>> combinationSum3(int k, int n) {backtrack(k,n,1,0);return ans;}public void backtrack(int k,int n,int startindex,int sum){if(path.size() == k){if(sum == n)ans.add(new ArrayList<>(path));return;}for(int i = startindex;i<=9;i++){sum += i;path.add(i);backtrack(k,n,i+1,sum);path.removeLast();sum -= i;}}
}
组合总和Ⅳ
题目设定:
数组元素没有重复的,元素可以无限使用,但是结果可以重复,顺序不同即可
这题我用回溯,超时了
用动态规划,用爬楼梯的思想
target为要上的楼梯的层数,nums[ ]为一次能上的层数
代码:
class Solution {public int combinationSum4(int[] nums, int target) {int[] dp = new int[target+1];dp[0] = 1;for(int i = 1;i<=target;i++){for(int j = 0;j<nums.length;j++){if(i-nums[j]>=0) dp[i] += dp[i-nums[j]];}}return dp[target];}
}
Leetcode 组合总和问题相关推荐
- Leetcode 组合总和 与 排列组合相关问题
Leetcode 组合总和 与 全排列相关问题 组合总和 题目链接: Leetcode 39.组合总和 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 c ...
- leetcode 组合总和(Java)
leetcode题目 组合总和 -- leetcode 39 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target , 找出 candidates 中所有可以使数字和为 ...
- leetcode 组合总和
39. 组合总和 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合.candidat ...
- Leetcode 组合总和II
组合总和II 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字 ...
- 《LeetCode力扣练习》第39题 组合总和 Java
<LeetCode力扣练习>第39题 组合总和 Java 一.资源 题目: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidate ...
- LeetCode 38外观数列39组合总和
维护公众号:bigsai 回复bigsai分享一些学习资源! 本周上篇 LeetCode 36有效的数独&37解数独(八皇后问题) 外观数列 给定一个正整数 n(1 ≤ n ≤ 30),输出外 ...
- LeetCode 216. 组合总和 III(排列组合 回溯)
1. 题目 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. 示例 1: 输 ...
- LeetCode 40. 组合总和 II(排列组合 回溯)
1. 题目 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
- 【LeetCode】【HOT】39. 组合总和(回溯)
[LeetCode][HOT]39. 组合总和 文章目录 [LeetCode][HOT]39. 组合总和 package hot;import java.util.ArrayList; import ...
最新文章
- win7怎么关闭虚拟机服务器,大师教您Win7系统怎么彻底关闭退出vmware虚拟机的处理要领...
- 《JAVA程序设计》第七周学习总结
- Hadoop之回收站
- linux之多任务的同步与互斥
- python文件图标变成小电脑_手把手教你给Python程序写图形界面,并且打包成exe文件-exe文件...
- 牛客网【每日一题】5月22日 [CQOI2009]中位数图
- 田渊栋:博士五年总结
- android sdk no space,Android SDK folder taking a lot of disk space. Do
- 【Windows】DACL SACL
- tp5连接mysql数据库_TP5的链接数据库
- 我的世界中国版服务器存档位置,我的世界中国版如何自己上传本地的mod和存档...
- 数据库是.frm,.myd,myi备份如何导入mysql (转)
- 每日算法系列【LeetCode 1006】笨阶乘
- CKEditor 实例
- SAP ABAP SD常用数据库表
- Delta RPMs disabled because /usr/bin/applydeltarpm not installed
- 解决steam饥荒联机版(DST Dont Starve Together)启动时error during initialization的方法
- Kubernetes kubeadm 证书到期,更新证书
- 点微同城小程序配置教程及提交审核包过审经验分享
- 必须做的事 教你数据备份