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或者和大于n
if(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;
}
}
}