Skip to content

Commit

Permalink
60 Permutation Sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
int32bit committed Apr 12, 2015
1 parent 23572e7 commit a151bec
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
+ [8 String to Integer (atoi)(溢出检测)](algorithms/StringtoInteger)
+ [21 Merge Two Sorted Lists](algorithms/MergeTwoSortedLists)
+ [31 Next Permutation](algorithms/NextPermutation)
+ [60 Permutation Sequence](algorithms/PermutationSequence)
+ [65 Valid Number(细节题,状态机)](algorithms/ValidNumber)
+ [191 Number of 1 Bits(位运算)](algorithms/Numberof1Bits)
+ [198 House Robber(DP)](algorithms/HouseRobber)
Expand Down
55 changes: 55 additions & 0 deletions algorithms/PermutationSequence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
```
"123"
"132"
"213"
"231"
"312"
"321"
```
Given n and k, return the kth permutation sequence.

**Note: Given n will be between 1 and 9 inclusive.**

## Solution

求康托展开式,具体算法见[康托展开算法](https://github.com/krystism/algorithms/tree/master/cantor)

由于是求第k个排列,而康托展开求的是根据比它小的数量,因此需要k减一

## Code

```c
char *listRemove(char *s, int i)
{
int n = strlen(s), j;
for (j = i; j < n - 1; ++j)
s[j] = s[j + 1];
s[j] = 0;
return s;
}
char *getPermutation(int n, int k) {
char *ans = malloc(sizeof(char) * (n + 1));
char *nums = malloc(sizeof(char) * n);
const int FAC[] = {1,1,2,6,24,120,720,5040,40320,362880};
for (int i = 0; i < n; ++i)
nums[i] = '0' + i + 1;
k--;
int cur = 0;
for (int i = n - 1; i > 0; --i) {
int index = k / FAC[i];
k %= FAC[i];
ans[cur++] = nums[index];
listRemove(nums, index);
}
ans[cur++] = nums[0];
ans[cur] = 0;
free(nums);
return ans;
}
```
36 changes: 36 additions & 0 deletions algorithms/PermutationSequence/permutationSequence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *listRemove(char *s, int i)
{
int n = strlen(s), j;
for (j = i; j < n - 1; ++j)
s[j] = s[j + 1];
s[j] = 0;
return s;
}
char *getPermutation(int n, int k) {
char *ans = malloc(sizeof(char) * (n + 1));
char *nums = malloc(sizeof(char) * n);
const int FAC[] = {1,1,2,6,24,120,720,5040,40320,362880};
for (int i = 0; i < n; ++i)
nums[i] = '0' + i + 1;
k--;
int cur = 0;
for (int i = n - 1; i > 0; --i) {
int index = k / FAC[i];
k %= FAC[i];
ans[cur++] = nums[index];
listRemove(nums, index);
}
ans[cur++] = nums[0];
ans[cur] = 0;
free(nums);
return ans;
}
int main(int argc, char **argv)
{
for (int i = 1; i <= 6; ++i)
printf("%s\n", getPermutation(3, i));
return 0;
}

0 comments on commit a151bec

Please sign in to comment.