Combination Sum (Java)

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.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • 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] 

注意,但凡不出现重复[2,2,3] [3,2,2]这种情况,都要设一个i的start传入递归。 这道题的解决方法和题目subset以及combinations类似。

Source

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> st = new ArrayList<List<Integer>>();
        List<Integer> a = new ArrayList<Integer>();
    	if(candidates.length == 0) return st;
    	int sum = 0;
    	int start = 0;
    	
    	Arrays.sort(candidates);  //这道题的测试数据有乱序的
    	dfs(start, sum, candidates, target, st, a);
    	return st;
    }
    public void dfs(int start, int sum, int[] candidates, int target, List<List<Integer>> st, List<Integer> a){
    	if(sum > target) return;
    	if(sum == target){
    		st.add(new ArrayList<Integer>(a));
    		return;
    	}
    	
    	for(int i = start; i < candidates.length; i++){
    		a.add(candidates[i]);
    		dfs(i, sum + candidates[i], candidates, target, st, a); //注意sum是要随着dfs变化的
    		a.remove(a.size() - 1);
    	}
    }
}


Test

    public static void main(String[] args){
    	int[] candidates = {2,3,6,7};
    	int target = 7;
    	System.out.println(new Solution().combinationSum(candidates, target));
    
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可以使用回溯法(Backtracking)来解决。回溯法是一种暴力搜索的算法,它通过不断尝试所有可能的情况来寻找问题的解。 在这个问题中,我们可以使用一个递归函数来进行回溯搜索。函数需要接收以下参数: - 数组arr:要搜索的数组 - int target:目标和 - int start:当前搜索的起始下标 - List<Integer> path:保存当前搜索路径的列表 - List<List<Integer>> result:保存所有搜索结果的列表 具体实现如下: ```java public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { path.add(arr[i]); // 将当前数加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前数从路径中移除 } } ``` 在上面的函数中,当目标和为0时,说明找到了一组解,将当前路径保存到结果列表中。如果目标和小于0或者已经搜索到数组末尾,直接返回。 在每次搜索时,我们从当前位置开始,依次将数组中的数加入路径中。然后递归搜索下一层,将目标和减去当前数。当搜索返回时,我们需要回溯,将当前数从路径中移除,以便尝试其他可能的组合。 下面是完整的代码: ```java import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { int[] arr = {2, 3, 6, 7}; int target = 7; List<List<Integer>> result = combinationSum(arr, target); System.out.println(result); } public static List<List<Integer>> combinationSum(int[] arr, int target) { Arrays.sort(arr); // 先对数组进行排序,以便剪枝 List<List<Integer>> result = new ArrayList<>(); findSum(arr, target, 0, new ArrayList<>(), result); return result; } public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { if (i > start && arr[i] == arr[i-1]) continue; // 剪枝,去掉重复的数 path.add(arr[i]); // 将当前数加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前数从路径中移除 } } } ``` 在上面的代码中,我们先对数组进行排序,以便剪枝,去掉重复的数。在搜索过程中,如果发现当前数和前一个数相同,直接跳过,避免重复计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值