1. import java.util.ArrayList;
  2. import java.util.List;
  3. /**
  4. * Source : https://oj.leetcode.com/problems/permutation-sequence/
  5. *
  6. *
  7. * The set [1,2,3,…,n] contains a total of n! unique permutations.
  8. *
  9. * By listing and labeling all of the permutations in order,
  10. * We get the following sequence (ie, for n = 3):
  11. *
  12. * "123"
  13. * "132"
  14. * "213"
  15. * "231"
  16. * "312"
  17. * "321"
  18. *
  19. * Given n and k, return the kth permutation sequence.
  20. *
  21. * Note: Given n will be between 1 and 9 inclusive.
  22. */
  23. public class PermutationSequence {
  24. /**
  25. * 寻找n个元素组成的第k个排列
  26. *
  27. * 第一方法:
  28. * 从最小的开始,循环k次找到第k个排列
  29. *
  30. * 第二种方法:
  31. * 找到第k个排列的规律
  32. *
  33. * 这种题目,可以先以一个简单的例子来找规律
  34. * 这里假设n = 4, k = 9
  35. * 1234
  36. * 1243
  37. * 1324
  38. * 1342
  39. * 1423
  40. * 1432
  41. * 2134
  42. * 2143
  43. * 2314 >>>> k = 9
  44. * 2341
  45. * 2413
  46. * 2431
  47. * 3124
  48. * 3142
  49. * 3214
  50. * 3241
  51. * 3412
  52. * 3421
  53. * 4123
  54. * 4132
  55. * 4213
  56. * 4231
  57. * 4312
  58. * 4321
  59. *
  60. * 以下认为arr下标从1开始
  61. * 第一位:
  62. * arr[4] = {1,2,3,4},k = 9,n = 4
  63. * 上面的排列是有规律的,每个位置每一个数字出现cycle = (n-1) = (4-1)! = 6次,整体第k个的时候,该位置已经经过 count = k / cycle = 1个周期,则该位置的数是arr[count + 1] = 2,因为已经过了count个周期,是第count+1个周期,就轮到了数组arr中的第count+1个数字
  64. *
  65. * 第二位:
  66. * arr[3] = {1,3,4},k' = k % (n-1)! = 3,n' = 3
  67. * 右面一个位置的数字,以2开头,因为2已经确定,剩下的形成一个新的数组arr[3] = {1,3,4},每个位置每个数字出现cycle = (n' - 1) = (3-1)! = 2次,整体第k个的时候,对于当前数组组成的排列而言处于第k' = k % (n-1) = 3个,第三个的第一位这个时候已经经过count = k' / cycle = 1个周期,正处于第index = count + 1 = 2个周期,也就是该位置的数为arr[index]
  68. *
  69. * 第三位:
  70. * arr[2] = {1,4},k'' = k' % (n'-1)! = 1,n'' = 2
  71. * cycle = (n'' - 1)! = 1, count = k'' / cycle = 1,index = count + 1 = 2, arr[index] = 4
  72. *
  73. * 第四位:
  74. * arr[1] = {1}, k''' = k'' % (n'' - 1)! = 0, n''' = 1
  75. * cycle = (n''' - 1)! = 1,index = k''' / cycle + 1 = 1,arr[index] = 1
  76. *
  77. * @param n
  78. * @param k
  79. * @return
  80. */
  81. public String getPermutation (int n, int k) {
  82. if (n < 1) {
  83. return "";
  84. }
  85. int[] fatorial = new int[n];
  86. List<Integer> arr = new ArrayList<Integer>();
  87. fatorial[0] = 1;
  88. arr.add(1);
  89. for (int i = 1; i < n; i++) {
  90. fatorial[i] = fatorial[i-1] * i;
  91. arr.add(i+1);
  92. }
  93. int index = 0;
  94. // 因为数组下标是从0开始的,k--就是为了让数组下标从0开始
  95. k--;
  96. StringBuilder result = new StringBuilder();
  97. while (n > 0) {
  98. index = k / fatorial[n-1];
  99. result.append(arr.get(index));
  100. arr.remove(index);
  101. k = k % fatorial[n-1];
  102. n--;
  103. }
  104. return result.toString();
  105. }
  106. public static void main(String[] args) {
  107. PermutationSequence permutationSequence = new PermutationSequence();
  108. System.out.println(permutationSequence.getPermutation(4, 9));
  109. System.out.println(permutationSequence.getPermutation(4, 24));
  110. }
  111. }

leetcode — permutation-sequence的更多相关文章

  1. [LeetCode] 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]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  3. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  4. LeetCode——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

    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 解题报告(Python & C++)

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

  7. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  8. 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 ...

  9. 【LeetCode练习题】Permutation Sequence

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

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

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

随机推荐

  1. PIL: 建立一个GIF图

    PIL: 建立一个GIF图 一.下载PIL库:   PIL库的下载是:pip install pillow(pillow就是PIL函数了) 二.采用以下代码(有注释): import PIL.Imag ...

  2. 更新windows补丁时一直卡在搜索更新

    在微软下载好安装补丁Windows8.1-KB2999226-x64后,双击时一直停留在“正在此计算机上搜索”界面. 解决方案: 1.将windows 自动更新设置为:“从不检查更新”  . 2.关闭 ...

  3. 高级查询query

    详细看 https://www.kancloud.cn/ldkt/tp5_db/229042

  4. C语言函数指针与 c#委托和事件对比

    C语言: 函数指针可以节省部分代码量,写类似具有多态的函数,比如要比较最大值,如果不用函数指针就只能写比较某一类型比如int类型的max函数,这个max无法比较string的大小.函数指针的意义就不多 ...

  5. drools规则引擎中易混淆语法分析_相互触发导致死循环分析

    整理了下最近在项目中使用drools出现的问题,幸好都在开发与测试阶段解决了,未波及到prod. 首先看这样两条规则: /** * 规则1_set默认利率a */ rule "rate_de ...

  6. C++代码审查---审查孙晓宁马踏棋盘谜题程序

    与孙晓宁同学结对审查,其代码地址如下:https://github.com/brunnhilder/-1/blob/master/%E9%A9%AC%E8%B8%8F%E6%A3%8B%E7%9B%9 ...

  7. 随便写写,也有一些参考了我jio的很好的他人的成果

    Spring框架学习记录(1) 一. https://www.cnblogs.com/yuanqinnan/p/10274934.html (一)只要用框架开发java,一定躲不过spring,Spr ...

  8. 我的C#跨平台之旅(四):使用AOP(filter、attribute)进行系统增强

    1.使用OData提速REST API开发 引入NuGet包:Microsoft.AspNet.WebApi.OData 在启动类中添加如下配置(示例为全局配置,也可基于Controller或Acti ...

  9. Android开发者的Anko使用指南(三)之资源

    添加依赖 dependencies { compile "org.jetbrains.anko:anko-commons:$anko_version" } Color 0xff00 ...

  10. Ettercap 实施中间人攻击

    中间人攻击(MITM)该攻击很早就成为了黑客常用的一种古老的攻击手段,并且一直到如今还具有极大的扩展空间,MITM攻击的使用是很广泛的,曾经猖獗一时的SMB会话劫持.DNS欺骗等技术都是典型的MITM ...