当前位置:   article > 正文

力扣------全排列_c语言全排列力扣

c语言全排列力扣

在这里插入图片描述

/**
 * 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().
 */
int **ar;
int v;
void backtracking(int* nums,int b,int numsSize){
    if(b==numsSize){
        for(int j=0;j<numsSize;j++){
            ar[v][j]=nums[j];
        }
        v++;
        return;
    }
    else{
        for(int i=b;i<numsSize;i++){
            int a=nums[i];
            nums[i]=nums[b];
            nums[b]=a;
            backtracking(nums,b+1,numsSize);
             a=nums[i];
            nums[i]=nums[b];
            nums[b]=a;
        }
    }
}
int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    int a=1;
    v=0;
    for(int b=1;b<=numsSize;b++){
        a=a*b;
    }
    *returnSize=a;
    
     (*returnColumnSizes) = (int*)malloc(sizeof(int) * (*returnSize));
   for (int i = 0; i < (*returnSize); i++) {
        (*returnColumnSizes)[i] = numsSize;
    }
    ar = (int**)malloc(sizeof(int*) * (*returnSize)); 
    for (int i = 0;i <(*returnSize);i++)
    {
        ar[i] = (int*)malloc(sizeof(int) * numsSize);
    }
    backtracking(nums,0,numsSize);
    return ar;
}
  • 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
  • 45
  • 46
  • 47
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/387022
推荐阅读
相关标签
  

闽ICP备14008679号