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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

解题思路:

之前曾在Java for LeetCode 046 PermutationsJava for LeetCode 031 Next Permutation做过关于字典排序的算法,但是可以肯定,这种近乎暴力枚举的算法肯定是通不过测试的!通过观察发现k / factorial(n - 1)其实就代表了str的第一个字母,顺着这个思路以此类推即可,JAVA实现如下:

  1. static public String getPermutation(int n, int k) {
  2. StringBuilder str = new StringBuilder();
  3. for (int i = 0; i < n; i++)
  4. str.append(i + 1);
  5. StringBuilder sb = new StringBuilder();
  6. while (n > 1) {
  7. int index = k / factorial(n - 1);
  8. k %= factorial(n - 1);
  9. if(k==0){
  10. index--;
  11. sb.append(str.charAt(index));
  12. str.deleteCharAt(index);
  13. sb.append(new StringBuilder(str).reverse());
  14. return sb.toString();
  15. }
  16. sb.append(str.charAt(index));
  17. str.deleteCharAt(index);
  18. n--;
  19. }
  20. return "1";
  21. }
  22.  
  23. static int factorial(int n) {
  24. if (n == 1)
  25. return 1;
  26. else
  27. return (factorial(n - 1) * n);
  28. }

Java for LeetCode 060 Permutation Sequence的更多相关文章

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

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

  2. 【LeetCode】060. 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】 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 ...

随机推荐

  1. Chrome商店Crx离线安装包下载

    第一步:找到Chrome的扩展应用ID 第二步:输入扩展应用ID 第三步:单击 生成 按钮. 第四步:在这里右键另存为即可下载.

  2. HDU-1754I Hate It 线段树区间最值

    这道题比较基本,就是用线段树维护区间最值,可以算是模板吧-.. I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768 ...

  3. 【uoj150】 NOIP2015—运输计划

    http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...

  4. QA要懂的Linux命令

    <一>软件安装相关QA经常需要安装测试软件(jmeter.Mock.python环境搭建.java环境搭建),或者配置测试环境(nginx.ci等),需要了解linux下如何安装软件.在工 ...

  5. unity 全屏乱影 BlitMultiTap

    http://m.blog.csdn.net/blog/stalendp/40859441 官方例子AngryBots的链接地址:http://u3d.as/content/unity-technol ...

  6. afasf

    http://www.cnblogs.com/ttzhang/archive/2008/11/02/1324601.html project server 2007 sn :W2JJW-4KYDP-2 ...

  7. jquery autocomplete 简单实用例子

    <link href="../../themes/default/css/jquery.ui.all.css" rel="stylesheet" type ...

  8. gvim设置成不备份文件

    打开gVim,进入“编辑”-“启动设定” 在“behave mswin”下行位置添加 set nobackup 语句 退出并保存配置文件 :wq

  9. logback 项目应用

    1.gradle引用: compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3' compile grou ...

  10. editplus快捷键大全之editplus编辑快捷键

    前面我们说了editplus快捷键大全之editplus文件快捷键和editplus快捷键大全之editplus光标快捷键,这里我们讲一下editplus快捷键大全之editplus编辑快捷键 删除光 ...