描述

2.1.13 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.
Note: Given n will be between 1 and 9 inclusive
大意:n个数的全排列,按照从下到大的顺序排列,问第k个位置的数是多少?

分析

  1. .用next_permutation来求,这个也是leetcode上的一道题
  2. 使用康托编码
    康托展开
    当前排列在n个不同元素的全排列的名次 【213】 在3个数的全排列中第三,公式:

    an表示第i个元素再未出现的元素中拍第几(从“0”开始)。举例:
    【4213】第一个元素为4,4在这四个元素中排第三;第二个元素“2”在剩下的三个元素中配第1;依次“1”第0,; “3”第0
    x = 3*3!+1*2! + 0*1!+0*1! = 20;(从0开始)
    康托展开的逆运算
    根据在总排列中的名次来确定这个排列,比如:
    【1234】这四个数的第20个排列是啥?利用辗转相除法确定康托展开中的系数,然后每次输出当前未出现过的第ak个元素。

代码

/*
date: 2018/3/15 16:05
des:
2.1.13 Permutation Sequence
The set [1,2,3,4 ,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.analyze: 求第n个排列
使用康托编码
X = an*(n-1)!+an-1(n-2)! + .. +a1*0!;
*/#include "type.h"int factorial(int n);
int permunationNum(const string &str);
vectorInt getPermution_1(int n,int k );
vectorInt getPermution_2(int n,int k );/*
利用康托展开求[str] 是第几个排列
*/
int permunationNum(const string &str)
{int len = str.length();int result = 0;int theNums = 0;for (int i=0; i<len; i++){theNums = 0;for (int j=i+1; j<len;j++)//遍历查看当前是第几个{if(str[i]>str[j])theNums++;}result += theNums*factorial(len-i-1);}return result;
}/*
利用康托编码的逆编码 求第k个排列
辗转相除
*/vectorInt getPermution_1(int n,int k )
{vectorInt result;vectorInt nuSet(n,0);int value = 0;for (int i=0; i<n; i++){if (k == 0){for (int j=0; j<n; j++){if (nuSet[j]==0){value = j+1;result.push_back(j+1);}}break;}int factorialNum = factorial(n-i-1);int tmp = factorialNum>0 ? k/factorialNum : 0;for (int j=0; i<n &&tmp>=0; j++){if (nuSet[j]){continue;}if (tmp==0){value = j+1;nuSet[j] = value;}tmp--;}result.push_back(value);k = k%factorialNum;}return result;
}/*
利用康托编码的逆编码 求第k个排列
辗转相除
k 第几个 从零开始
n 几个数 1~n
*/vectorInt getPermution_2(int n,int k )
{vectorInt result;vectorInt seq(n,0);for (int i=0; i<n; i++){seq[i] = i+1;}int counts = factorial(n);//总数if (counts<k)//判断k的合法性{return result;}for (int i=0; i<n; i++){counts /= n - i; //n-i 补位零result.push_back(seq[k/counts]);//counts 不能为零seq.erase(seq.begin()+k/counts);//删除已选数k %= counts;}return result;
}

参考

康拓编码——Permutation Sequence相关推荐

  1. 【数字全排列】LeetCode 60. Permutation Sequence

    LeetCode 60. Permutation Sequence Solution0: 偷鸡摸狗的做法 class Solution {public:string getPermutation(in ...

  2. 60 Permutation Sequence

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

  3. Permutation Sequence

    2019独角兽企业重金招聘Python工程师标准>>> The set [1,2,3,-,n] contains a total of n! unique permutations. ...

  4. LeetCode60:Permutation Sequence

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

  5. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. 60. Permutation Sequence

    一.题目 1.审题 2.分析 给两个数字 n 与 k,返回 1-n 所有数字组成的从小到大的全排序的第 k 个数. 二.解答 1.思路: 方法一.采用字典序列,返回全部序列后,输出第 k 个.(时间超 ...

  7. LeetCode Permutation Sequence(求排列中的第k个排列是什么)

    思路:使用阶乘,f(n) =  f(n -1) * n,在确定第i个数是,用k/f(n - 1 - i) 代码如下: public class Solution {public String getP ...

  8. Leetcode Permutation Sequence

    超时算法-排序dfs: class Solution { public: string s; string s1; int count;string getPermutation(int n, int ...

  9. LeetCode刷题(35)--Permutation Sequence

    对于第一个数字,剩余共有(n-1)!中排列,因此第一个数字为k/(n-1)!,之后的数字依次类推 import math class Solution(object):def getPermutati ...

最新文章

  1. 微生物培养的福音:一个直接用16S rDNA序列来预测其培养基配方的网站
  2. vs2015使用GIt连接git.oschina.net/
  3. 恭喜神策数据客户即刻完成 C 轮融资
  4. CVPR 2017论文集锦
  5. 【机器学习】GBDT 与 LR 的区别总结
  6. 解决eclipse 中文乱码问题
  7. numpy matlab 索引不同,与Numpy相似的MATLAB数组索引
  8. IOS-网络(GCD)
  9. windows7 系统优化大技巧
  10. c语言字符比较思路,C语言讲解思路资料
  11. python监控网页更新_python监控网页更新
  12. Rust 多久更新一次?
  13. LINUX下载编译lame
  14. 关于LVDS的硬件总结
  15. 程序员薪水差距在哪里?
  16. python opencv图像笔记
  17. [转]杀毒软件的引擎
  18. 计算机管理信息阶段性测验答案,管理信息系统阶段性学习测验一试题及答案(14页)-原创力文档...
  19. P2002 消息扩散(图论 Tarjan缩点)
  20. 使用OpenCV和Python进行人脸识别

热门文章

  1. 【项目】08年度科技创新项目
  2. Android--中国象棋
  3. 图像分割之大津法Otsu
  4. java的renameTo函数的两个用法,这个看不懂算我输
  5. FZU - 2302  Necklace (dp+斜率优化)
  6. 李开复 给地震孩子们的一封信
  7. UnityShader——Phong光照模型 = Diffuse + Specular + Ambient
  8. Flink重写Iceberg数据湖小文件变大文件
  9. python与办公自动化专业就业方向_自动化专业就业方向
  10. 蒙特卡罗亚式期权定价matlab,[转载]亚式期权定价-——蒙特卡罗方法介绍(二)...