全遍历,得到子集的方法:
1,位操作法:类似哈夫曼树的遍历,比如3个数,遍历组合有:000,001,010,011,100,101,110,111
int oneInNum(int n) { int i=0,sum=0; while(n & 0xFFFFFFFF) { if(1 & n) { sum+=1; } n=n>>1; } return sum; } int** subsets(int* nums, int numsSize, int** columnSizes, int* returnSize) { *returnSize=1<<numsSize; int **arr=(int **)malloc(sizeof(int *)*(*returnSize)); int *colSize=(int *)malloc(sizeof(int)*(*returnSize)); int i=0,j=0; for(i=0;i<*returnSize;i++) { colSize[i]=oneInNum(i); if(colSize[i]==0) arr[i]=(int *)malloc(sizeof(int)* 1); else arr[i]=(int *)malloc(sizeof(int)* colSize[i]); int cnt=0; for(j=0;j<numsSize;j++) { if(i& 1<<j) { arr[i][cnt++]=nums[j]; } } } columnSizes=&colSize; return arr; }
2,递归回溯法:深度优先遍历,看如下链接
http://blog.csdn.net/u012501459/article/details/46777141