Share my c solution


  • 1
    F
    enter code here
    

    int** permute(int* nums, int numsSize, int* returnSize) {
    int **res;
    int i;
    *returnSize=1;
    for(i=numsSize;i>0;i--)
    *returnSize=returnSizei;
    res=(int **) malloc(sizeof(int )(*returnSize));
    for(int i=0;i<(*returnSize);i++)
    res[i]=(int *)malloc(sizeof(int)*numsSize);
    int x[numsSize],k=0,m=0,flag=0;
    x[k]=-1;
    while(k>=0){
    x[k]++;
    while(x[k]<numsSize){
    for(i=0;i<k;i++) {
    if(x[i]==x[k]){
    flag=1;
    break;
    }
    } //for
    if(flag==1){
    x[k]++;
    flag=0;
    }
    else{
    for(i=0;i<=k;i++)
    res[m][i]=nums[x[i]];
    if(k==numsSize-1){
    m++;
    break;
    }
    k++;
    x[k]=0;
    }
    }//while
    k--;
    }//while
    return res;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.