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

题目:

LeetCode:60. 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的集合(n = 3 ,[1,2,3]),寻找它的kth个全排列子列。

分析:

方法一: 利用STL中的next_permutation函数实现

特点:代码简洁,暴力枚举

方法二: 利用康托逆展开的方式进行快速寻找,关于康托展开

特点:快速有效

代码:

  1. string getPermutationEx(int n, int k)
  2. {
  3. string s(n, '0');
  4. for (int i = 0; i < n; ++i)
  5. {
  6. s[i] = i + 1;
  7. }
  8. for (int i = 0; i < k - 1; ++i)
  9. {
  10. next_permutation(s.begin(), s.end());
  11. }
  12. return s;
  13. }
  1. string getPermutation(int n, int k)
  2. {
  3. string strTemp(n, '0');
  4. string strRes;
  5. for (int i = 0; i < n; ++i)
  6. {
  7. strTemp[i] += i + 1;
  8. }
  9. int nNum = 1;
  10. int nTemp = n;
  11. while (0 != --nTemp)
  12. {
  13. nNum *= nTemp;
  14. }
  15. int kTemp = k - 1;
  16. int nA;
  17. nTemp = n - 1;
  18. for (auto iterBg = strTemp.begin(); iterBg != strTemp.end();)
  19. {
  20. nA = kTemp / nNum; // a = k / (n - 1)!;
  21. kTemp = kTemp % nNum; // k = k % (n - 1)!;
  22. strRes.push_back(strTemp[nA]);
  23. strTemp.erase(iterBg + nA);
  24. nNum = nNum / (nTemp ? nTemp : 1);
  25. --nTemp;
  26. }
  27. return strRes;
  28. }

/***********************2017年5月3日更新*******************************************/

测试代码:

  1. // test for Permutation Sequence
  2. int main()
  3. {
  4. string s = getPermutation(1, 1);
  5. for (int i = 0; i < s.size(); ++i)
  6. {
  7. printf("%c", s[i]);
  8. }
  9. }

备注:

此处对康托逆展开做一个说明:

为了寻找到 n = 5 , k = 6的子序列步骤如下:

  1. n = 5,则说明初始序列为“12345”,使用a1、a2、a3、a4、a5表示;
  2. 根据康托逆展开中描述,该序列变化了k = k - 1 = 5次,即在 “12345“的第五个序列
  3. a1 = k / (n - 1)! = 5 / 4! = 0; // 整除 第一位数字为比“1”大0的数字“1”
  4. k1 = k % (n - 1)! = 5 % 4! = 5; // k1 作为下一次运算的k 带入算式
  5. a2 = k1 / (n - 2)! = 5 / 3! = 0; //整除 第二位数字为比“2”大0的数字,“1” 已经被“取”走所以此处取“2”
  6. k2 = k1 % (n - 2)! = 5 % 3! = 5; // k2 作为下一次运算的k 带入算式
  7. a3 = k2 / (n - 3)! = 5 / 2! = 2; //整除 第三位数字为比“3”大2的数字,(“1”“2” 已经被“取”走)此处取“5”
  8. k3 = k2 % (n - 3)! = 5 % 2! = 1; // k3 作为下一次运算的k 带入算式
  9. a4 = k2 % (n - 4)! = 1 / 1 = 1; // 第四位数字为比“3”大1的数字,(“1”“2” 已经被“取”走)此处取“4”
  10. k4 = k3 % (n - 4)! = 5 % 2! = 0; // k3 作为下一次运算的k 带入算式
  11. a5 为剩余的 “3”;// 当然程序设计的时候只需要对 (n - i)!进行非0处理就可以了,不需要单独进行循环外处理。

算法逻辑:

  1. 通过n,k创建初始化的 strTemp;
  2. 开始寻找第K序列;// 第k = k - 1个
  1. 寻找a1 ; // a = k / (n - 1)!;
  2. k = k % (n - 1)!;
  3. 将a存入 输出数据strRes中;
  4. 移除str[a1]元素

    重复上述。

谢谢小伙伴提的意见,后续博客会更新leetcode相关内容。本来不打算写下来的,毕竟leetcode题目博客在网上一大抄,但是个人还是觉得吸取大家的意见,顺路巩固加强下自己的理解,好记性不如烂笔头!

LeetCode:60. Permutation Sequence,n全排列的第k个子列的更多相关文章

  1. [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 ...

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

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

  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: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  6. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  7. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  8. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. RabbitMQ集群搭建

    准备三个节点,系统为CentOS7 Node IP rabbitmq01 172.50.0.64 rabbitmq02 172.50.0.65 rabbitmq03 172.50.0.66 这里把no ...

  2. 彻底取消Myeclipse对js文件的校验

    这个是我在网上看到的,自己也做一下笔记. 一般情况下,关掉校验:Window -->Preferences -->MyEclipse -->单击Validation,这样不一定有效. ...

  3. 老李分享:网页爬虫java实现

    老李分享:网页爬虫java实现   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821 ...

  4. TextField和TextView

    本文概要 1.简介 2.介绍TextField控件 3.介绍TextView控件 4.键盘的打开和关闭 5.关闭和大开键盘的通知 6.键盘的种类 详情 1.简介 与Label一样,TextField和 ...

  5. mysql 分析3使用分析sql 性能 show profiles ;

    show variables like '%profiling%';    查看状态  查看时间去哪了``` set  profiling=1;// 打开 show profiles;  查看执行过的 ...

  6. 01 The Learning Problem

    什么时候适合用机器学习算法? 1.存在某种规则/模式,能够使性能提升,比如准确率: 2.这种规则难以程序化定义,人难以给出准确定义: 3.存在能够反映这种规则的资料. 所以,机器学习就是设计算法A,从 ...

  7. java集合基础

    集合概念与作用 1现实生活中把很多事物凑在一起就是集合.java中的集合类:是一种工具,就像是容器,存储任意数量的有共同属性的对象. 2在类的内部,对数据进行组织: 简单而快速的搜索大数量的条目 有的 ...

  8. 什么是RESTful?

    RESTful一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. REST ...

  9. [Oracle]审计Audit

    1.Audit的概念 Audit是监视和记录用户对数据库进行的操作,以供DBA进行问题分析.利用Audit功能,可以完成以下任务: 监视和收集特定数据库活动的数据.例如管理员能够审计哪些表被更新,在某 ...

  10. IOS——触摸事件 视图检测和事件传递

    iPhone上有非常流畅的用户触摸交互体验,能检测各种手势:点击,滑动,放大缩小,旋转.大多数情况都是用UI*GestureRecognizer这样的手势对象来关联手势事件和手势处理函数.也有时候,会 ...