#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void dfs(int *nums, int size, int start, int *stack,
int len, int **sets, int *count, int *sizes)
{
int i;
sets[*count] = malloc(len * sizeof(int));
memcpy(sets[*count], stack, len * sizeof(int));
sizes[*count] = len;
(*count)++;
for (i = start; i < size; i++) {
stack[len] = nums[i];
dfs(nums, size, i + 1, stack, len + 1, sets, count, sizes);
}
}
/**
** Return an array of arrays of size *returnSize.
** The sizes of the arrays are returned as *returnColumnSizes array.
** Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
**/
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
{
int capacity = 5000;
int **sets = malloc(capacity * sizeof(int *));
int *stack = malloc(numsSize * sizeof(int));
*returnColumnSizes = malloc(capacity * sizeof(int));
*returnSize = 0;
dfs(nums, numsSize, 0, stack, 0, sets, returnSize, *returnColumnSizes);
return sets;
}
int main(int argc, char **argv)
{
int i, j;
if (argc <= 1) {
fprintf(stderr, "Usage: ./test array...\n");
exit(-1);
}
int size = argc - 1;
int *nums = malloc(size * sizeof(int));
for (i = 0; i < size; i++) {
nums[i] = atoi(argv[i + 1]);
}
int *sizes;
int count;
int **lists = subsets(nums, size, &count, &sizes);
for (i = 0; i < count; i++) {
for (j = 0; j < sizes[i]; j++) {
printf("%d ", lists[i][j]);
}
printf("\n");
}
return 0;
}
DdddJMs__135
- 粉丝: 2737
- 资源: 646
最新资源
- 报告-华为IPD项目管理“六步一法”
- 市场模式下光伏用户群的电能共享与需求响应模型 关键词:光伏用户群;定价;需求响应;纳什均衡;分布式优化 仿真软件:matlab
- JAVA源码PDF文档字体处理FontBox
- 消费者退单风险分析模型
- H3C G3服务器P430/H460/P460系列阵列卡的RAID配置方法
- VxWorks6.8安装指南及文件(全).zip
- JAVA源码P2P应用程序协议框架JavaBEEPCore
- 《MySQL数据库项目式教程》项目三++数据定义.ppt
- 暴风电视 50X4 ECHO 屏V500DJ6-QE1(T4) 机编60000AM7902 屏参30173301 V1.0.20
- H3C G3服务器UEFI模式下RSTe板载软RAID阵列配置教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈