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函数实现

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

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

特点:快速有效

代码:

string getPermutationEx(int n, int k)
{
string s(n, '0');
for (int i = 0; i < n; ++i)
{
s[i] = i + 1;
}
for (int i = 0; i < k - 1; ++i)
{
next_permutation(s.begin(), s.end());
} return s;
}
string getPermutation(int n, int k)
{
string strTemp(n, '0');
string strRes;
for (int i = 0; i < n; ++i)
{
strTemp[i] += i + 1;
} int nNum = 1;
int nTemp = n;
while (0 != --nTemp)
{
nNum *= nTemp;
} int kTemp = k - 1;
int nA; nTemp = n - 1;
for (auto iterBg = strTemp.begin(); iterBg != strTemp.end();)
{
nA = kTemp / nNum; // a = k / (n - 1)!;
kTemp = kTemp % nNum; // k = k % (n - 1)!;
strRes.push_back(strTemp[nA]);
strTemp.erase(iterBg + nA); nNum = nNum / (nTemp ? nTemp : 1);
--nTemp;
} return strRes;
}

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

测试代码:

    // test for Permutation Sequence
int main()
{
string s = getPermutation(1, 1);
for (int i = 0; i < s.size(); ++i)
{
printf("%c", s[i]);
}
}

备注:

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

为了寻找到 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. Linux下Scala(2.12.1)安装

    一.文件准备 1.1 文件名称 scala-2.12.1.tgz 1.2 下载地址 http://www.scala-lang.org/download/2.12.1.html 二.工具准备 2.1 ...

  2. 请一定记得升级java虚拟机

    对于吃货出身又需要保持体重的我,出门一定要带男票,因为这样就可以把见到的好吃的都买给他吃,就当是自己吃了[汗].偶尔做梦还是会梦到自己一个角落里偷吃东西,听到有脚步声,抬起头,大哭起来:“我饿了.”  ...

  3. 【Yii系列】最佳实践之后台业务框架

    缘起 上面的几章都讲概念了,没有怎么讲到实践的东西,可能会有些枯燥,这很正常的,概念还是需要慢慢啃的,尤其是官网其他的部分,需要狠狠的啃. 什么,你啃不动了?看看官网旁边的那个在线用户吧. 你不啃的时 ...

  4. PRINCE2重要性--光环国际培训

    项目的重要性 答:对于当今的组织来说,一个关键的挑战,就是能够成功地平衡以下两个并存的.互相竞争的方面:保持现有的商业运营--盈利能力.服务质量.客户关系.品牌忠实度.生产效率.市场信心等,这些被称为 ...

  5. 光环国际的PRINCE2培训是怎么上课的?

    一.面授班级: 基础级:3天知识精讲(含案例分享+现场演练) 上课时间:上午9:00-12:00,下午1:30-4:30分 考试时间:第3天课程结束后5:30-6:30分 1 个小时闭卷考试   专业 ...

  6. jQuery习题

    1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? 答:$("div:has(span)&q ...

  7. 测试开发Python培训:实现屌丝的图片收藏愿望(小插曲)

    测试开发Python培训:实现屌丝的图片收藏愿望(小插曲) 男学员在学习python的自动化过程中对于爬虫很感兴趣,有些学员就想能收藏一些图片,供自己欣赏.作为讲师只能是满足愿望,帮助大家实现对美的追 ...

  8. Spring+Redis(keyspace notification)实现定时任务(订单过期自动关闭)

    1.起因 最近公司项目要做订单超期未支付需自动关闭,首先想到的是用spring的定时器(@Schedule),结果领导举各种例子说会影响性能,只能作罢.后来想能不能基于redis实现, 学习(baid ...

  9. Android -- Annotation(注解)原理详解及常见框架应用

    1,我们在上一篇讲到了EventBus源码及3.0版本的简单使用,知道了我们3.0版本是使用注解方式标记事件响应方法的,这里我们就有一个疑问了,为什么在一个方法加上类似于"@Subscrib ...

  10. 启动LUXContentTests过程中遇到的问题

    首先,要想在localbox中使用Selenium,就得准备好浏览器的driver文件.比如chrome对应的chromedriver文件,该文件是一个exe可执行文件. 问题:当我尝试去跑LUXCo ...