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.
数组[1,2,3,4,...n]共有n!中不同的排列,给定整数n和k, 返回n!个有序的排列中的第k个排列
由于排列是有序的,我们可以以此缩小搜索第K个排列的范围。
class Solution {
public:
int getCaseCount(int n){
//计算n!
if(n==0)return 1;
int result=1;
for(int i=1; i<=n; i++){
result*=i;
}
return result;
}
string getPermutation(int n, int k) {
int size=getCaseCount(n);
if(k<1 || k>size)return "";
vector<int>candidates;
for(int i=1; i<=n; i++)candidates.push_back(i);
int pos=1; //当前确定第几位数
int totalCount=0; //已经确定了前多少个排列,用于缩小范围
string result="";
while(pos<=n){
size=getCaseCount(n-pos); //确定当前值
int index=1; //当前位置要放candidates集合中的第几个数
while(totalCount+index*size<k)index++;
//填写当前数
result+= (char)(candidates[index-1]+'0');
//从候选集合中删除当前数
candidates.erase(candidates.begin()+index-1);;
//更新范围
totalCount+=size*(index-1);
pos++;
}
return result;
}
};LeetCode: Permutation Sequence [059],布布扣,bubuko.com
LeetCode: Permutation Sequence [059]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/26820145