• LeetCode //C - 46. Permutations


    46. Permutations

    Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
     

    Example 1:

    Input: nums = [1,2,3]
    Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

    Example 2:

    Input: nums = [0,1]
    Output: [[0,1],[1,0]]

    Example 3:

    Input: nums = [1]
    Output: [[1]]

    Constraints:
    • 1 <= nums.length <= 6
    • -10 <= nums[i] <= 10
    • All the integers of nums are unique.

    From: LeetCode
    Link: 46. Permutations


    Solution:

    Ideas:

    The basic idea behind this solution is a recursive backtracking approach to generate all possible permutations for the input array nums. At each step of the recursion, we consider each number in the array for the current position and then recursively generate permutations for the remaining numbers.

    Key Components:

    1. swap function:
    This is a simple utility function that swaps two numbers in the array.

    2. generatePermutations function:
    This function is where the main logic resides. It takes the following parameters:

    • nums: The input array.
    • numsSize: The size of the input array.
    • start: The starting index for generating permutations.
    • result: The 2D array where we store all the permutations.
    • returnSize: A pointer to an integer that keeps track of the number of permutations generated so far.

    The logic works as follows:

    • If start is equal to numsSize, it means we have a valid permutation of the array. So, we copy the current permutation into the result array and increment the returnSize.
    • Otherwise, for each index i from start to numsSize-1, we swap the numbers at indices start and i, and then recursively call generatePermutations with start + 1. After the recursive call, we swap the numbers back (backtrack) to restore the original array order. This ensures that we explore all possible configurations for the array.

    3. permute function:
    This is the main function that is called by the user. It sets up the necessary data structures and then calls generatePermutations to generate all permutations. The steps are:

    • Calculate the maximum number of permutations possible for an array of size numsSize. This is simply numsSize! (factorial of numsSize).
    • Allocate memory for the result 2D array based on the maximum number of permutations.
    • Allocate memory for the returnColumnSizes array and set all its values to numsSize since all permutations will have the same size.
    • Call generatePermutations to fill the result array with all permutations.
    • Return the result array.
    Code:
    /**
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *returnColumnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
    void swap(int* nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    
    void generatePermutations(int* nums, int numsSize, int start, int** result, int* returnSize) {
        if (start == numsSize) {
            result[*returnSize] = (int*)malloc(numsSize * sizeof(int));
            for (int i = 0; i < numsSize; i++) {
                result[*returnSize][i] = nums[i];
            }
            (*returnSize)++;
            return;
        }
        for (int i = start; i < numsSize; i++) {
            swap(nums, start, i);
            generatePermutations(nums, numsSize, start + 1, result, returnSize);
            swap(nums, start, i); // backtrack
        }
    }
    
    int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
        int maxPermutations = 1;
        for (int i = 1; i <= numsSize; i++) {
            maxPermutations *= i;
        }
    
        int** result = (int**)malloc(maxPermutations * sizeof(int*));
        *returnSize = 0;
        *returnColumnSizes = (int*)malloc(maxPermutations * sizeof(int));
        for (int i = 0; i < maxPermutations; i++) {
            (*returnColumnSizes)[i] = numsSize;
        }
    
        generatePermutations(nums, numsSize, 0, result, returnSize);
        
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    C语言:扫雷小游戏
    java spring cloud 企业电子招标采购系统源码:营造全面规范安全的电子招投标环境,促进招投标市场健康可持续发展
    房屋租赁管理系统
    【Java技术专题】「Java8技术盲区」让我们来看看新一代IO流的开发指引(流升级功能体系)
    用echarts显示本年365天每天的数据
    华为L3VPNv4 over SRv6 BE配置案例
    近代科学的诞生
    Ubuntu空间不足,如何扩容
    Qt5开发从入门到精通——第四篇八节(进度条)
    Numpy-01(安装、Ndarray对象介绍创建及使用)
  • 原文地址:https://blog.csdn.net/navicheung/article/details/133734237