您的位置:首页 > 产品设计 > UI/UE

leetcode 60: Permutation Sequence

2015-08-02 14:15 447 查看
class Solution {
public:
string getPermutation(int n, int k) {
string res;
int divider[10];/
4000
/save 1! 2! ... (n-1)!
bool visited[10]={0};//mark the numbers are already used
divider[1]=1;
for(int i=2;i<=n;i++)
divider[i]=divider[i-1]*i;
k--;//this is for the calculation of every number
for(int i=1;i<=n;i++)
{
int count=k/divider[n-i]+1;
int j;
for(j=1;j<=n;j++)
{
if(!visited[j])
count--;
if(!count)
break;
}
visited[j]=1;
res+=j+'0';
k%=divider[n-i];
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: