【leetcode】48. 全排列 2

 

int cmp(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}
void recursion(int* nums, int numsSize, int* returnSize, int* returnColumnSizes, int cnt, int** arr, int* temp, int* hash){
    if (cnt == numsSize){
        arr[(*returnSize)] = (int*)calloc(numsSize + 1, sizeof(int));
        memcpy(arr[(*returnSize)], temp, cnt*sizeof(int));
        returnColumnSizes[(*returnSize)++] = cnt;
        return;
    }
    for (int i = 0; i<numsSize; i++){
        if (hash[i]) continue;
        hash[i]++;
        temp[cnt] = nums[i];
        recursion(nums, numsSize, returnSize, returnColumnSizes, cnt + 1, arr, temp, hash);
        hash[i]--;
        while (i != numsSize-1 && nums[i] == nums[i + 1])
            i++;
    }
}
int** permuteUnique(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    int** arr = (int**)calloc(sizeof(int*), 1000);
    int* hash = (int*)calloc(sizeof(int), numsSize);
    *returnSize = 0;
    *returnColumnSizes = (int*)calloc(sizeof(int), 1000);
    int temp[1000] = { 0 };
    qsort(nums, numsSize, sizeof(int), cmp);
    recursion(nums, numsSize, returnSize, *returnColumnSizes, 0, arr, temp, hash);
    return arr;
}

 

上一篇:【leetcode】102. 二叉树的层序遍历


下一篇:Leetcode 589. N叉树的前序遍历(DAY 3)