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.
解题思路,采用标准库的next_premutation()进行枚举到第K个排列。
Code:
string getPermutation(int n,int k){
string(n,'0');
for(int i=0;i<n;i++){
s[i]+=i+1;
}
for(int i=0;i<k-1;++i){
next_permutation(s.begin(),s.end());
}
return s;
}
本文介绍如何使用递归和标准库函数解决排列序列问题,通过实例展示从给定集合中找到第K个排列的方法。
366

被折叠的 条评论
为什么被折叠?



