【leetcode】77. 组合

 

void recursion(int n, int k, int* returnSize, int* col,int* temp,int cnt,int** arr,int start){
    if(cnt==k){
        arr[(*returnSize)]=(int*)calloc(k,sizeof(int));
        memcpy(arr[(*returnSize)],temp,k*sizeof(int));
        col[(*returnSize)++]=k;
        return;
    }    
    for(int i=start; i<=n-k+1+cnt; i++){
        temp[cnt]=i;
        recursion(n, k, returnSize, col, temp, cnt+1, arr,i+1);
    }
}
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
    double x1=n, x2=n-k, val1=1, val2=1, val=1;
    while(x1>k) 
        val1*=x1--;
    while(x2>1) 
        val2*=x2--;
    int** arr=(int**)calloc(val1/val2,sizeof(int*));
    *returnSize=0;
    *returnColumnSizes=(int*)calloc(val1/val2,sizeof(int*));
    int temp[100]={0};
    recursion(n,k,returnSize,*returnColumnSizes,temp,0,arr,1);
    return arr;
}

 

posted @ 2020-12-12 17:51  温暖了寂寞  阅读(78)  评论(0编辑  收藏  举报