From a151bec3c057b0d75a773bcd2ff5394509c48bfb Mon Sep 17 00:00:00 2001 From: krystism Date: Sun, 12 Apr 2015 11:17:23 +0800 Subject: [PATCH] 60 Permutation Sequence --- README.md | 1 + algorithms/PermutationSequence/README.md | 55 +++++++++++++++++++ .../PermutationSequence/permutationSequence.c | 36 ++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 algorithms/PermutationSequence/README.md create mode 100644 algorithms/PermutationSequence/permutationSequence.c diff --git a/README.md b/README.md index 591bc1c..dbcad19 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/algorithms/PermutationSequence/README.md b/algorithms/PermutationSequence/README.md new file mode 100644 index 0000000..36819d9 --- /dev/null +++ b/algorithms/PermutationSequence/README.md @@ -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; +} +``` diff --git a/algorithms/PermutationSequence/permutationSequence.c b/algorithms/PermutationSequence/permutationSequence.c new file mode 100644 index 0000000..a502e8a --- /dev/null +++ b/algorithms/PermutationSequence/permutationSequence.c @@ -0,0 +1,36 @@ +#include +#include +#include +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; +}