赞
踩
/** * 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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。