-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp046.c
50 lines (50 loc) · 1.5 KB
/
p046.c
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
48
49
50
/**
* Return an array of arrays of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int** permute(int* nums, int numsSize, int* returnSize) {
if (numsSize == 0) return 0;
*returnSize = 1;
int i, j, k;
for (i = 1; i < numsSize; i++)
{
*returnSize *= (i+1);
}
int *index = (int*)malloc(sizeof(int)*numsSize);
for (i = 0; i < numsSize; i++)
index[i] = i;
int **result = (int**)malloc(sizeof(int*)*(*returnSize));
for (i = 0; i < *returnSize; i++)
{
result[i] = (int*)malloc(sizeof(int)*numsSize);
for (j = 0; j < numsSize; j++)
{
result[i][j] = nums[index[j]];
}
for (j = numsSize-1; j > 0; j--)
{
if (index[j-1] < index[j])
{
for (k = j+1; k < numsSize; k++)
{
if (index[k] < index[j-1])
{
break;
}
}
k--;
index[j-1] ^= index[k];
index[k] ^= index[j-1];
index[j-1] ^= index[k];
for (k = j; k < (j+numsSize)>>1; k++)
{
index[k] ^= index[j+numsSize-1-k];
index[j+numsSize-1-k] ^= index[k];
index[k] ^= index[j+numsSize-1-k];
}
break;
}
}
}
return result;
}