求数组中任意个数的组合

/**
 * 所有组合
 * @author Q.Yuan
 *
 */
public class Combination1 {
	/**
	 * 求a中count个数的组合
	 * @param a          存放组合的数     
	 * @param restCount  当前还需多少个数构成一个组合
	 * @param count      在a中挑选出count个数
	 * @param b			   存放当前组合
	 * @param pos        指向a中的元素,
	 */
	public void combination(int []a,int restCount,int count,int[] b,int pos){
		if(restCount <= 0 || count > a.length){
			return;
		}
		for(int i = pos;i >= restCount - 1;i-- ){
			b[restCount - 1] = a[i];
			if(restCount - 1 == 0){
				for(int j:b){
					System.out.print(j);
				}
				System.out.println();
			}
			combination(a, restCount-1, count, b, i-1);
		}
	}
	public static void main(String[] args) {
		Combination1 c = new Combination1();
		int[] a = {1,2,3,4};
		//挑选出任意两个数的组合
		final int count = 3;
		int[] b = new int[count];
		c.combination(a, count, count, b, a.length - 1);
	}
}

这个问题可以使用回溯法(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、付费专栏及课程。

余额充值