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;
}