LC60 Permutation Sequence

这道题目有两种做法,第一种是把排列一直列到第k个。另一种做法是利用康托编码,如果把n!个排列根据首个数字大小排列成n组,则每一组有(n-1)!个元素,求第k个排列,可以先令p=k/(n-1)! 求出第一个数字num[p].

然后递归做下去。更新数字k=k%(n-1)!,并在数组num中删除num[p]。如果把(n-1)!个排列根据第二个数字大小排列成(n-1)组,则每一组有(n-2)!个元素,求第k个排列,可以令p=k/(n-2)! 求出第二个数字num[p].

这样直到数组num中只有一个元素为止。

要特别注意,康托编码的第k个排列是从0开始数的。

 1 class Solution {
 2 public:
 3     string getPermutation(int n, int k) {
 4         string s="";
 5         if(n<=0||k<=0)
 6             return s;
 7         int tmp=1;
 8         string str(n,‘0‘);
 9         for(int i=1;i<=n;i++)
10         {
11             str[i-1]+=i;
12             tmp*=i;
13         }
14         tmp=tmp/n;
15         k--;
16         for(int i=n-1;i>0;i--)
17         {
18             int p=k/tmp;
19             k=k%tmp;
20             s+=string(1,str[p]);
21             str.erase(str.begin()+p);
22             tmp=tmp/i;
23         }
24         s+=string(1,str[0]);
25         return s;
26     }
27 };

时间: 2024-10-08 20:50:39

LC60 Permutation Sequence的相关文章

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" "3

60. 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" "3

[leetcode]Permutation Sequence

问题描述: The set [1,2,3,-,n] contains a total ofn! 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" &

每日算法之四十二:Permutation Sequence (顺序排列第k个序列)

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" "

Leetcode 60. 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): Given n and k, return the kth permutation sequence. Note: Given n will be between

【leetcode】 Permutation Sequence (middle)

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" "3

[C++]LeetCode: 114 Permutation Sequence(返回第k个阶乘序列——寻找数学规律)

题目: 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" &q

【LeetCode】Permutation Sequence

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" &q

LeetCode 59 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" "