LeetCode 47. 全排列 II Permutations II(C语言)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hang404/article/details/85210946

题目描述:

给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

题目解答:

方法1:回溯

因为有重复元素,所以需要先排序,然后进行回溯。每次将没有添加过的元素加入临时数组,相等的则直接跳过,注意要过滤掉重复元素。
运行时间8ms,代码如下。

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int comp(const void* a, const void* b) {
    return *(int*)a - *(int*)b;
}
void dfs(int*** result, int* size, int* nums, int n, int* before, int bef, bool* flag) {
    if(bef == n) {
        
        (*size)++;
        result[0] = (int**)realloc(result[0], *size * sizeof(int*));
        result[0][*size - 1] = (int*)malloc(n * sizeof(int));
        memcpy(result[0][*size - 1], before, n * sizeof(int));
        return;
    }
    int i = 0;
    for(i = 0; i < n; i++) {
        if(flag[i])
            continue;
        // if (i > 0 && nums[i] == nums[i - 1] && !flag[i - 1])
        //    continue; 	//可以把下边的while循环换成这两句话。
        flag[i] = true;
        before[bef] = nums[i];
        dfs(result, size, nums, n, before, bef + 1, flag);
        flag[i] = false;
        while(i + 1 < n && nums[i] == nums[i + 1])
            i++;
    }
}
int** permuteUnique(int* nums, int numsSize, int* returnSize) {
    qsort(nums, numsSize, sizeof(int), comp);
    int** result = NULL;
    int* before = (int*)malloc(numsSize * sizeof(int));
    bool* flag = (bool*)calloc(numsSize, sizeof(bool));
    dfs(&result, returnSize, nums, numsSize, before, 0, flag);
    free(before);
    free(flag);
    return result;
}

交换法,因为每次交换之后的序列,有可能是无序的,但又有重复元素,所以需要每次交换前都需要先进行排序。
运行时间8ms,代码如下。

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int comp(const void* a, const void* b) {
    return *(int*)a - *(int*)b;
}
void dfs(int*** result, int* size, int* nums, int n, int start) {
    if(start + 1 == n) {
        (*size)++;
        result[0] = (int**)realloc(result[0], *size * sizeof(int*));
        result[0][*size - 1] = (int*)malloc(n * sizeof(int));
        memcpy(result[0][*size - 1], nums, n * sizeof(int));
        return;
    }
    int i = 0;
    int* temp = (int*)malloc(n * sizeof(int));
    memcpy(temp, nums, n * sizeof(int));
    qsort(nums + start, n - start, sizeof(int), comp);
    for(i = start; i < n; i++) {          
        int t = nums[start];
        nums[start] = nums[i];
        nums[i] = t;
        dfs(result, size, nums, n, start + 1);
        nums[i] = nums[start];
        nums[start] = t;
        while(i + 1 < n && nums[i] == nums[i + 1])
            i++;
    }
    memcpy(nums, temp, n * sizeof(int));
    free(temp);
}
int** permuteUnique(int* nums, int numsSize, int* returnSize) {
    qsort(nums, numsSize, sizeof(int), comp);
    int** result = NULL;
    dfs(&result, returnSize, nums, numsSize, 0);
    return result;
}

猜你喜欢

转载自blog.csdn.net/hang404/article/details/85210946