求第k个排列。

刚开始按照一个排列一个排列的求,超时。

于是演算了一下,发下有数学规律,其实就是康托解码。

康托展开:全排列到一个自然数的双射

X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!

ai为整数,并且0<=ai<i(1<=i<=n)

适用范围:没有重复元素的全排列

全排列的解码

如何找出第16个(按字典序的){1,2,3,4,5}的全排列?

1. 首先用16-1得到15

2. 用15去除4! 得到0余15

3. 用15去除3! 得到2余3

4. 用3去除2! 得到1余1

5. 用1去除1! 得到1余0

有0个数比它小的数是1,所以第一位是1

有2个数比它小的数是3,但1已经在之前出现过了所以是4

有1个数比它小的数是2,但1已经在之前出现过了所以是3

有1个数比它小的数是2,但1,3,4都出现过了所以是5

最后一个数只能是2

所以排列为1 4 3 5 2

class Solution{
public:
string getPermutation(int n, int k)
{
//get fractial
vector<int> fractial;
fractial.push_back();
for(int i = ;i<n;i++)
{
fractial.push_back(fractial[i-]*(i+));
}
//to mark if this digit selected ,true means can be selected, false means already selected.
vector<bool> allnum;
for(int i = ; i <=n; i++)
allnum.push_back(true); int ChuShu = k - , YuShu = ;
string ans;
int weishu = ; while(weishu<n)
{
int _num_i;
int place;
if(weishu == n-) // the last digit
_num_i = select(allnum,);
else
{
YuShu = ChuShu % fractial[n--weishu];
place = ChuShu / fractial[n--weishu]; _num_i = select(allnum,place + );
}
ChuShu = YuShu;
weishu ++;
char ch = '' - + _num_i;
ans += ch;
}
return ans;
}
int select(vector<bool> &allnum,int place)
{
int i = ; while(place)
{
if(allnum[i] == true)
{
place--;
if(place == )
break;
}
i++;
}
allnum[i] = false;
return i;
}
}; int main()
{
class Solution myS;
cout<<myS.getPermutation(,);
return ;
}

LeetCode OJ--Permutation Sequence *的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [LeetCode] 60. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. 【leetcode】 Permutation Sequence

    问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...

随机推荐

  1. [51nod] 1301 集合异或和

    考虑不限制xor{Y}>xor{X} 考虑n=m的情况,每个数i∈[1,n]可以被分配到X集合或Y集合,或不分配 设f[S]表示{X} xor {Y} == S的方案数 有f[S]+=2*f[S ...

  2. JAVA基础篇—Servlet小结

    一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...

  3. #pragma与_Pragma(转载)

    C90为预处理指令家族带来一位新成员:#pragma.一般情况下,大家很少见到它.        #pragma的作用是为特定的编译器提供特定的编译指示,这些指示是具体针对某一种(或某一些)编译器的, ...

  4. POJ:3160-Father Christmas flymouse

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  5. c#中利用“|”运算组合多项

    前几天看到一段代码 int i = GetCount(para1 | para2); 咋一看有些莫名奇妙,怎么传参的时候带了个或运算,其实这里面是有讲究的,查阅了各方资料,QQ群里赖着大牛问,才搞明白 ...

  6. configurationChanges

    在Android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置.在activity加上android:c ...

  7. ogre3D学习基础7---材质详解

    物体着色的基础 --- 四种不同光照作用 1.环境反射 近似的模拟了场景中的全局辐射,也就是用来近似模拟所有光在场景中不断散射的结果.材质中有相应的属性来代表这种环境反射颜色. 2.漫反射 这种颜色是 ...

  8. python 学习分享-购物车实操篇

    程序要求如下: '''购物车程序: 启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就 ...

  9. 01 Java 代码是怎么运行的

    Java代码运行的方式 1:在开发工具中运行 2:双击 jar 文件运行 3:在命令行中运行 4:在网页中运行 上述运行方式都离不开 JRE,也就是 Java 运行时环境.实际上 JRE 仅包含运行 ...

  10. TransH中的Hinge Loss Function

    Hinge Loss Function Hinge Loss 函数一种目标函数,有时也叫max-margin objective. 在Trans系列中,有一个 \[ \max(0,f(h,r,t) + ...