78. 子集(C语言)回溯法

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
	int** ret = malloc(10000);
	int* buf = malloc(10000);
	*returnSize = 0;
	*returnColumnSizes = malloc(10000);
	backtrack(ret, buf, 0,0, nums, numsSize, returnSize, returnColumnSizes);
	return ret;

}
void backtrack(int** ret, int* buf, int pos,int j, int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
	if (pos == numsSize) {
		if (j >= 0) {
			ret[*returnSize] = malloc(numsSize * 4);
			memcpy(ret[*returnSize], buf, 4 * j);
			(*returnColumnSizes)[*returnSize] = j;
			(*returnSize)++;
			j = 0;
			return;
		}
	}
	else {
		
        backtrack(ret, buf, pos + 1, j, nums, numsSize, returnSize, returnColumnSizes);
        buf[j] = nums[pos];
        backtrack(ret, buf, pos + 1, j + 1, nums, numsSize, returnSize, returnColumnSizes);
	}

}

 

你可能感兴趣的:(力扣题目)