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

这个疑问点后面再研究。

相关推荐
tudousisi22212 分钟前
P2758 编辑距离 题解复盘
算法
李可以量化24 分钟前
Tornado 从了解到精通(四)上:实战搭建 Web 应用与核心组件详解
算法
linux-hzh1 小时前
百日算法修炼 · Day 09
数据结构·算法·排序算法
DFT计算杂谈1 小时前
Janus单层Cr2SSe中的应变可调多压电效应与谷电子学
人工智能·算法·机器学习
今天AI了吗1 小时前
从 LLM 到 Agent Skill:把 AI 底层概念串起来
数据库·人工智能·sql·深度学习·神经网络·算法·机器学习
hanlin031 小时前
动态规划专练:力扣第1035、392题
算法·leetcode·动态规划
淡海水1 小时前
C#与常用数据结构源码剖析-全篇导览
开发语言·数据结构·unity·面试·职场和发展·c#
不正经学生2 小时前
C语言大小端字节序:内存里字节的排列顺序
c语言·开发语言·arm开发·c++·算法·c#
大模型探索者2 小时前
2026金融大模型训推平台选型指南:主流厂商横向对比与私有化落地
人工智能·算法·金融
动词ing2 小时前
【C语言】命名规范
c语言·开发语言