039 - Combination Sum

原创 2015年11月19日 17:07:41

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 (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 2,3,6,7 and target 7,
A solution set is:
[7]

[2, 2, 3]


给定一个数列,给定一个target,找出数列中的几个数,这几个数的和为target,

数列中的数可以重复使用

先排序,去除重复的数字,再递归求值,要注意,下一个取出来的数不能比之前取出来的数小,用left变量来判断

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 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 = 0; i < nsize; i++) {
		tmp[(*tmpsize)++] = nlist[i];
		dofun(nlist + i, nsize - i, target - nlist[i], ret, col, retsize, tmp, tmpsize);
		(*tmpsize)--;
	}
}

int **combinationSum(int *nlist, int nsize, int target, int **col, int *retsize)
{
	qsort(nlist, nsize, sizeof(int), mycompar);
	int i, k=0, past=-1;
	for (i = 0; i < nsize; i++) {
		if (nlist[i] != past)
			nlist[k++] = nlist[i];
		past = nlist[i];
	}
	nsize = k;

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


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

LeetCode039 Combination Sum

详细见:leetcode.com/problems/combination-sum Java Solution: github package leetcode; import java.ut...
  • zxwtry
  • zxwtry
  • 2017年04月05日 15:47
  • 110

[LeetCode] 039. Combination Sum (Medium) (C++)

[LeetCode] 039. Combination Sum (Medium) (C++)
  • hcbbt
  • hcbbt
  • 2015年08月08日 08:23
  • 1505

LeetCode 39. Combination Sum II

和LeetCode 38. Combination Sum

39-Combination Sum

题目:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniqu...

Leetcode|Combination Sum III[回溯]

Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

LeetCode_40---Combination Sum II

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

DFS-leetcode Combination Sum I/I I

深度优先搜索(DFS)是搜索算法的一种。最早接触DFS应该是在二叉树的遍历里面,二叉树的先序、中序和后序遍历实际上都属于深度优先遍历,实质就是深度优先搜索,后来在图的深度优先遍历中则看到了更纯正的深度...

leetcode【39+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合,使得组合中数字的和是target值。并且组合里的数字可以重复,也就是不限制C中每一个数字的使用。 这明显是回溯啦,大家记得让...

LeetCode39 Combination Sum

原题:英:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uni...

【LeetCode】 Combination Sum 系列

Combination Sum 【LeetCode】39.Combination Sum 【LeetCode】40.Combination Sum2
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:039 - Combination Sum
举报原因:
原因补充:

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