LeeCode 40.组合总和II

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

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

**注意:**解集不能包含重复的组合。

示例 1:

复制代码
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

复制代码
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

答案如下:

cpp 复制代码
void zuhe2(int target, int idx, int* temp, int tempSize
	, int** res, int*** pRes, int* returnSize, int** returnColumnSizes, int* pCapacity, int* numCountMap);

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) { // LeetCode 40.组合和总和
        // 先统计数组中每个数的出现个数。由题意 1 <= candidates[i] <= 50, numCountMap[n]表示n在candidates数组中出现的次数
    int numCountMap[51] = { 0 };
    for (int i = 0; i < candidatesSize; i++) {
        numCountMap[candidates[i]]++;
    }
    // 1 <= target <= 30 , 而数组candidates中的数最小为1,所以组合子数组长度最大可能为30
    // 但是不知道组合数量,所以结果数组大小不确定。
    int tempCapacity = candidatesSize < 30 ? candidatesSize : 30;
    int* temp = (int*)malloc(tempCapacity * sizeof(int)); 
    if (!temp) return NULL;
    *returnSize = 0;
    int capacity = 2; // 初始容量,后续容量不够再扩容

    *returnColumnSizes = (int*)malloc(capacity * sizeof(int));
    int** res = (int**)malloc(capacity * sizeof(int*));
    if (!res) return NULL;
    zuhe2(target, 0, temp, 0, res ,&res ,returnSize, returnColumnSizes, &capacity, numCountMap);
    free(temp);
    return res;
}

void zuhe2(int target, int idx, int* temp, int tempSize
	, int** res, int*** pRes, int* returnSize, int** returnColumnSizes, int* pCapacity, int* numCountMap) {
	if (target == 0) {
		// 已满足,保存结果
		int* arr_ = (int*)malloc(tempSize * sizeof(int));
		if (!arr_) return;
		memcpy(arr_, temp, tempSize * sizeof(int));
		// 保存前先检查容量
		if (*returnSize == *pCapacity) { // 之前申请的内存满了,不够再存储了,扩容
			int newCapacity = *pCapacity << 1;
			int** temp_res = (int**)realloc(res, newCapacity * sizeof(int*));
			if (!temp_res) return;
			*pRes = temp_res;

			*returnColumnSizes = (int*)realloc(*returnColumnSizes, newCapacity * sizeof(int));
			if (*returnColumnSizes == NULL) return;

			*pCapacity = newCapacity;
		}

		*(*pRes + *returnSize) = arr_;
		*(*returnColumnSizes + *returnSize) = tempSize; // 该组合的数字个数
		*returnSize = *returnSize + 1;
		return;
	}
	for (int i = idx; i < 51; i++) {
		// 先判断i是否使用过了
		if (numCountMap[i] == 0) {
			// 该数使用完了
			continue;
		}
		if (i > target) {
			break;
		}
		// 可以加的情况,选中该数
		temp[tempSize++] = i;
		numCountMap[i]--;
		// 再递归选给临时数组下一位赋值。 注意,这里idx参数值必选传i,不能传idx,防止选到重复的组合。组合的临时数组中当前在选的数还可以选,但前面选过的数不能再选
		zuhe2(target - i, i, temp, tempSize, res, pRes, returnSize
			, returnColumnSizes, pCapacity, numCountMap);
		// 回退,不选这个数了
		tempSize--;
		numCountMap[i]++;
	}
}

测试代码:

cpp 复制代码
void testLeeCode40(void) { // 组合总和II
	int nums[] = { 4,4,2,1,4,2,2,1,3 };
	int target = 6;
	int numsSize = sizeof(nums) / sizeof(int);
	int returnSize; // 用于接受结果二维数组的长度。
	int* returnColumnSizes; // 用来接受结果二维数组的每个元素(即子数组)的长度
	int** res = combinationSum2(nums, numsSize, target, &returnSize, &returnColumnSizes);
	printArr(res, returnSize, returnColumnSizes); // 打印二维数组

	// 释放内存
	for (int i = 0; i < returnSize; i++) {
		free(res[i]);
	}
	free(res);
	free(returnColumnSizes);
}

运行:

ok. 但是提交到LeeCode,报错:

看上去是调用realloc函数时报错重复释放内存了。 但是没看出来哪里重复释放内存了。没懂这个报错哪来的,我自己机器上没报错。怎么办? 我把二维数组的容量直接设置为1000,1000应该是够用了,避免了动态扩容数组,提交通过了:

这个疑问点后面再研究。

相关推荐
_OP_CHEN16 分钟前
数据结构(C语言篇):(十三)堆的应用
c语言·数据结构·二叉树·学习笔记·堆排序··top-k问题
听情歌落俗1 小时前
MATLAB3-1变量-台大郭彦甫
开发语言·笔记·算法·matlab·矩阵
量子炒饭大师1 小时前
收集飞花令碎片——C语言关键字typedef
c语言·c++·算法
澡点睡觉1 小时前
【数据结构与算法Trip第4站】摩尔投票法
算法
cellurw1 小时前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http
..过云雨2 小时前
03.【Linux系统编程】基础开发工具1(yum软件安装、vim编辑器、编辑器gcc/g++)
linux·c语言·笔记·学习
青草地溪水旁2 小时前
Linux 高性能 I/O 事件通知机制的核心系统调用—— `epoll_ctl`
linux·c语言·c++
靠近彗星2 小时前
2.1线性表
数据结构
island13143 小时前
【Redis#9】其他数据结构
数据结构·数据库·redis
行走的bug...3 小时前
用图论来解决问题
算法·图论