/**
* 算法思想:
* 经典回溯算法,递归返回的条件为临时栈中的元素个数为k个;
*
*
*
*
**/
void rec(int n, int k, int index, int *stack, int *stack_index, int *ret_index, int **ret){
if(*stack_index == k){
ret[*ret_index] = (int *)malloc(sizeof(int) * k);
memcpy(ret[*ret_index], stack, sizeof(int) * k);
(*ret_index)++;
return;
}
for(; index<=n; index++){
stack[(*stack_index)++] = index;
rec(n, k, index+1, stack, stack_index, ret_index, ret);
(*stack_index)--;
}
}
#define LEN 0xfffff
int** combine(int n, int k, int* rs, int** colsize){
int **ret = (int **)malloc(sizeof(int *) * LEN);
int *stack = (int *)malloc(sizeof(int) * LEN);
int stack_index = 0;
int *cs = (int *)malloc(sizeof(int) * LEN);
int ret_index = 0;
rec(n, k, 1, stack, &stack_index, &ret_index, ret);
*colsize = cs;
int i;
for(i=0; i<ret_index; i++){
cs[i] = k;
}
printf("ret_index = %d\n", ret_index);
*rs = ret_index;
return ret;
}
LeetCode-77-组合-C语言
最新推荐文章于 2023-06-27 15:36:59 发布