题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @param numLen int num数组长度 * @return int整型二维数组 * @return int* returnSize 返回数组行数 * @return int** returnColumnSizes 返回数组列数 */ void back( int* num, int numLen, int** res, int* path, int pathsize, int* used, int* len ){ if(pathsize == numLen){ int *temp = (int *)malloc(sizeof(int) * numLen); memcpy(&temp[0], &path[0], sizeof(int) * numLen); res[len[0]] = temp; len[0]++; return; } for(int i = 0 ; i < numLen ; i++){ if(used[i] == 1) continue; path[pathsize] = num[i]; used[i] = 1; back(num,numLen,res,path,pathsize + 1,used,len); used[i] = 0; } } int** permute(int* num, int numLen, int* returnSize, int** returnColumnSizes ) { // write code here if(numLen == 0) return NULL; int sum = 1; for(int i = 1 ; i <= numLen ; i++) sum *= i; *returnSize = sum; int* col = (int*)malloc(sizeof(int)*sum); for(int i = 0 ; i < sum ; i++) col[i] = numLen; *returnColumnSizes = col; int* path = (int*)malloc(sizeof(int)*numLen); int* used = (int*)malloc(sizeof(int)*numLen); int** res = (int**)malloc(sizeof(int*)*sum); int len = 0; for(int i = 0 ; i < numLen ; i++){ used[i] = 0; } back(num,numLen,res,path,0,used,&len); return res; }