LeetCode第40题思悟——组合总数II(combination-sum-ii)

本文深入解析LeetCode第40题“组合总数II”的解题思路与算法实现,探讨数组排序、分治思想及递归应用,提供两种高效解法并分析差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode第40题思悟——组合总数II(combination-sum-ii)

知识点预告

  1. 数组的排序处理;
  2. 分治思想的应用;
  3. 递归结果的返回处理;

题目要求

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combination-sum-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

示例

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combination-sum-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的思路

这道题和上道题是相同类型的,只是增加了每个数字只能使用一遍,于是我们需要增加一个跳跃操作;

public static List<List<Integer>> combinationSum(int[] candidates, int target) {
	if(candidates.length==0){
		return new ArrayList<>();
	}
	Arrays.sort(candidates);
	return combinationSum(candidates,target,0);
}
private static boolean contains(int[] candidates,int start,int end,int target){
	int left=start,right=end;
	int middle;
	while(left<=right){
		middle=(left+right)/2;
		if(candidates[middle]<target){
			left=middle+1;
		}else if(candidates[middle]>target){
			right=middle-1;
		}else{
			return true;
		}
	}
	return false;
}
private static List<List<Integer>> combinationSum(int[] candidates,int target,int start){
	List<List<Integer>> result=new ArrayList<>();
	List<List<Integer>> subResult;
	int index=start;
	int nextIndex=index+1;
	int headValue=candidates[index];
	int realTarget=target-candidates[index];
	List<Integer> item;
	if(contains(candidates,start,candidates.length-1,target)){
		item=new ArrayList<>();
		item.add(target);
		result.add(item);
	}
	if(nextIndex<candidates.length) {
		while (headValue <= realTarget) {
			subResult = combinationSum(candidates, realTarget, nextIndex);
			if (subResult.size() != 0) {
				for (List<Integer> answer : subResult) {
					answer.add(headValue);
					result.add(answer);
				}
			}
			while(index<candidates.length&&candidates[index]==headValue){
				index++;
			}
			nextIndex=index+1;
			if (nextIndex < candidates.length) {
				headValue = candidates[index];
				realTarget = target - headValue;
			} else {
				break;
			}
		}
	}
	return result;
}

优秀解法

List<List<Integer>> result = new ArrayList<>();
// c:给定数组
// index:当前从index开始
// t:目标和
// l:结果
void f(int[] c,int index,int t,List<Integer> l){
	if (t == 0){
		// 找到了一个组合方式
		result.add(new ArrayList<>(l));
		return;
	}
	for (int i = index; i < c.length && c[i] <= t; i++){
		if (i != index && c[i] == c[i - 1]) continue;
		// 下一个位置
		l.add(c[i]);
		f(c,i + 1,t - c[i],l);
		l.remove(l.size() - 1);
	}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
	if (candidates == null || candidates.length == 0) return result;
	Arrays.sort(candidates);
	f(candidates,0,target,new ArrayList<>());
	return result;
}

差异分析

因为题目要求的变化,所以都是需要在上一题的基础上加上跳跃操作;另外,对于题目中说的,“candidates 中的每个数字在每个组合中只能使用一次”,那么candidates中是否存在重复数字?比如,出现2个1,那么1在结果中是允许出现2次还是1次呢?从通过的提交来看,应该还是只能出现1次;

至于这个问题,如果在面试中,还是需要确定一下的,否则直接index++不就好啦;

知识点小结

  1. 数组的排序处理;
  2. 分治思想的应用;
  3. 递归结果的返回处理;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值