[leetcode] 回溯法 Combination Sum 系列问题

73人阅读 评论(0)

leetcde中回溯法：具体的方法，我认为手把手教你 < leetcode > 中的回溯算法——多一点套路这篇博文讲的很清晰。

39 . Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]

public class Solution {
List<List<Integer>> result = new ArrayList<>();//保存最后结果

public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
if(target < candidates[0] || candidates.length == 0 || candidates == null){
return result;
}
List<Integer> list = new ArrayList<>();
backTracking(candidates, target, 0, list);

return result;
}

public void backTracking(int[] candicates, int target, int start, List<Integer> list){
if(target < 0){
return;
}else if(target == 0){
}else{
for(int i = start; i < candicates.length; i++){
backTracking(candicates, target - candicates[i], i, list);
list.remove(list.size() - 1);
}
}

}
}

40 . Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

public class Solution {
List<List<Integer>> result = new ArrayList<>();

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
if(candidates[0] > target || candidates.length == 0 || candidates == null){
return result;
}
List<Integer> list = new ArrayList<>();
backTracking(candidates, target, 0, list);

Set<List<Integer>> set = new HashSet<>(result);
return new ArrayList<>(set);
}

public void backTracking(int[] candidates, int target, int start, List<Integer> list){
if(target < 0){
return;
}else if(target == 0){
}else{
for(int i = start; i < candidates.length; i++){
backTracking(candidates, target - candidates[i], i + 1, list);
list.remove(list.size() - 1);
}
}
}
}

216 . Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

public class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
if(n < k ||n >= k * 9){
return result;
}
int[] candidates = new int[9];
for(int i = 0; i < 9; i++){
candidates[i] = i + 1;
}
List<Integer> list = new ArrayList<>();
backTracking(candidates,k, n, 0, list);
return result;
}

public void backTracking(int[] candidates, int k,int n, int start, List<Integer> list){
if(n < 0){
return;
}else if( n == 0){
if(list.size() == k)
}else{
int len = candidates.length;

for(int i = start; i < len; i++){
backTracking(candidates, k, n - candidates[i], i + 1, list);
list.remove(list.size() - 1);
}
}
}
}
0
0

【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐（算法+实战）--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：6738次
• 积分：662
• 等级：
• 排名：千里之外
• 原创：63篇
• 转载：1篇
• 译文：0篇
• 评论：1条
文章分类
阅读排行
最新评论