题目描述:
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
题目解答:
方法1:回溯算法
循环加递归。临时数字每更新一次数字就存储一次。
运行时间ms,代码如下。
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void dfs(int*** result, int* size, int** column, int* nums, int n, int* use, int used, int start) {
int i = 0;
for(i = start; i < n; i++) {
use[used] = nums[i];
(*size)++;
column[0] = (int*)realloc(column[0], *size * sizeof(int));
column[0][*size - 1] = used + 1;
result[0] = (int**)realloc(result[0], *size * sizeof(int*));
result[0][*size - 1] = (int*)malloc((used + 1) * sizeof(int));
memcpy