040 - Combination Sum II

原创 2015年11月19日 17:12:14

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


同39题,只是这次数列中的值不能重复使用,用一个数组来标记是否已使用过

int mycompar(const void *a, const void *b)
{
	int *x = (int *)a;
	int *y = (int *)b;
	if (*x > *y) return 1;
	return *x == *y? 0 : -1;
}

void dofun(int *nlist, int nsize, int *nmark, int left, int target, int **ret, int *col, 
		int *retsize, int *tmp, int *tmpsize)
{
	int i;

	if (target==0) {
		//tmp[(*tmpsize)++] = nlist[0];
		int *line = (int *)calloc(sizeof(int), *tmpsize);
		col[*retsize] = *tmpsize;
		memcpy(line, tmp, *tmpsize * sizeof(int));
		ret[*retsize] = line;
		(*retsize)++;
		return;
	}
	if (target < nlist[0]) return;
	for (i = left + 1; i < nsize; i++) {
		if (nmark[i]) continue;

		nmark[i] = 1;
		tmp[(*tmpsize)++] = nlist[i];
		int left = i;
		dofun(nlist, nsize, nmark, left, target - nlist[i], ret, col, retsize, tmp, tmpsize);
		(*tmpsize)--;
		nmark[i] = 0;
		while (nlist[i+1] == nlist[i]) i++;
	}
}

int **combinationSum2(int *nlist, int nsize, int target, int **col, int *retsize)
{
	qsort(nlist, nsize, sizeof(int), mycompar);

	int **ret = (int **)calloc(sizeof(int *), 8192);
	*col = (int *)calloc(sizeof(int), 8192);
	int tmp[8192] = {0};
	int tmpsize = 0;
	int nmark[8192] = {0};
	int left = -1;
	dofun(nlist, nsize, nmark, left, target, ret, *col, retsize, tmp, &tmpsize);
	return ret;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[leetcode] 040. Combination Sum II (Medium) (C++)

[leetcode] 040. Combination Sum II (Medium) (C++)
  • hcbbt
  • hcbbt
  • 2015年08月08日 08:33
  • 1440

leetcode -- Combination Sum II -- 重点

https://leetcode.com/problems/combination-sum-ii/与Combination Sum一样,只是每个元素只能用一次。子节点集合不包括本身就行。还是要排序,排...
  • xyqzki
  • xyqzki
  • 2015年12月14日 14:04
  • 211

DAY19:leetcode #40 Combination Sum II

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

LeetCode—*Combination Sum II(DFS算法C数组中有重复值)

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

[leetcode] 40. Combination Sum II

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

leetcode:Combination Sum(I,II)

原题: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C...

leetcode-java-40. Combination Sum II

/* Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w...

40-m-Combination Sum II

求指定和的组合,但个个数只能用一次。求和一的变种,有了一的经验,二式可以沿用一式的backtracking格式,由于每个数只能用一次,那么控制参数i每次递增1就可以了。另一个要注意的是可能有重复项而重...

[LeetCode]problem 40. Combination Sum II

TAG递归回溯组合数和link方法与Combination Sum 相比, 每个数仅能被使用一次 候选集不再是set,而是collections,就是可以包含重复数字了 怎么办呢?针对第一个,非常简单...

leetcode——Combination Sum II

子集问题的应用
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:040 - Combination Sum II
举报原因:
原因补充:

(最多只允许输入30个字)