题解 | #有重复项数字的全排列#
有重复项数字的全排列
https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @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(i != 0 && num[i - 1] == num[i] && used[i - 1] == 0) continue; 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 cmp(const void* a , const void* b){ return *(int*)a - *(int*)b; } int** permuteUnique(int* num, int numLen, int* returnSize, int** returnColumnSizes ) { // write code here if(numLen == 0) return NULL; int* path = (int*)malloc(sizeof(int)*numLen); int* used = (int*)malloc(sizeof(int)*numLen); int** res = (int**)malloc(sizeof(int*)*1000); int len = 0; for(int i = 0 ; i < numLen ; i++){ used[i] = 0; } qsort(num , numLen , sizeof(int) , cmp); back(num,numLen,res,path,0,used,&len); printf("%d\n" , len); *returnSize = len; *returnColumnSizes = (int*)malloc((*returnSize) * sizeof(int)); for(int i = 0; i < *returnSize; i++) { (*returnColumnSizes)[i] = numLen; } free(path); free(used); return res; }