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应该是够用了,避免了动态扩容数组,提交通过了:

这个疑问点后面再研究。

相关推荐
Ldawn_AI2 小时前
4+ 图论高级算法
算法·深度优先·图论
Xの哲學2 小时前
Linux PCI 子系统:工作原理与实现机制深度分析
linux·网络·算法·架构·边缘计算
NuyoahC3 小时前
笔试——Day46
c++·算法·笔试
Keying,,,,4 小时前
力扣hot100 | 图论 | 200. 岛屿数量、994. 腐烂的橘子、207. 课程表、208. 实现 Trie (前缀树)
算法·leetcode·图论
啊吧怪不啊吧4 小时前
C++之list类的代码及其逻辑详解 (中)
开发语言·数据结构·c++·list
.YM.Z4 小时前
数据在内存中的存储
c语言·内存
cwplh5 小时前
Codeforces1043 A至F 题解
算法
楼田莉子5 小时前
C++算法学习专题:滑动窗口
开发语言·数据结构·c++·学习·算法·leetcode
2501_924731115 小时前
智慧矿山误报率↓83%!陌讯多模态融合算法在矿用设备监控的落地优化
人工智能·算法·目标检测·视觉检测
特立独行的猫a5 小时前
C/C++三方库移植到HarmonyOS平台详细教程(补充版so库和头文件形式)
c语言·c++·harmonyos·napi·三方库·aki