题目

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.

Note: Given n will be between 1 and 9 inclusive.

题解:

发现数学规律。

首先先捋捋这道题要干啥,给了我们n还有k,在数列 1,2,3,... , n构建的全排列中,返回第k个排列。

题目告诉我们:对于n个数可以有n!种排列;那么n-1个数就有(n-1)!种排列。

那么对于n位数来说,如果除去最高位不看,后面的n-1位就有 (n-1)!种排列。

所以,还是对于n位数来说,每一个不同的最高位数,后面可以拼接(n-1)!种排列。

所以你就可以看成是按照每组(n-1)!个这样分组。

利用 k/(n-1)! 可以取得最高位在数列中的index。这样第k个排列的最高位就能从数列中的index位取得,此时还要把这个数从数列中删除。

然后,新的k就可以有k%(n-1)!获得。循环n次即可。

同时,为了可以跟数组坐标对其,令k先--。

代码如下:

 1     public String getPermutation(int n, int k) {  
 2         k--;//to transfer it as begin from 0 rather than 1
 3         
 4         List<Integer> numList = new ArrayList<Integer>();  
 5         for(int i = 1; i<= n; i++)
 6             numList.add(i);
 7        
 8         int factorial = 1;    
 9         for(int i = 2; i < n; i++)  
             factorial *= i;    
         
         StringBuilder res = new StringBuilder();
         int times = n-1;
         while(times>=0){
             int indexInList = k/factorial;
             res.append(numList.get(indexInList));  
             numList.remove(indexInList);  
             
             k = k%factorial;//new k for next turn
             if(times!=0)
                 factorial = factorial/times;//new (n-1)!
             
             times--;
         }
         
         return res.toString();
     } 

Reference:

http://blog.csdn.net/linhuanmars/article/details/22028697

http://blog.csdn.net/fightforyourdream/article/details/17483553

http://blog.csdn.net/u013027996/article/details/18405735

Permutation Sequence leetcode java的更多相关文章

  1. Permutation Sequence [LeetCode]

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

  2. Longest Consecutive Sequence leetcode java

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

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

  4. [leetcode]Permutation Sequence @ Python

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

  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] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

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

  7. 【LeetCode练习题】Permutation Sequence

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

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

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

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

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

随机推荐

  1. 图的遍历 之 深搜dfs

    DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...

  2. Codeforces.997C.Sky Full of Stars(容斥 计数)

    题目链接 那场完整的Div2(Div1 ABC)在这儿.. \(Description\) 给定\(n(n\leq 10^6)\),用三种颜色染有\(n\times n\)个格子的矩形,求至少有一行或 ...

  3. W3wp.exe占用CPU及内存资源

    问题背景 最近使用一款系统,但是经常出现卡顿或者用户账号登录不了系统.后来将问题定位在了服务器中的“w3wp.exe”这个进程.在我们的用户对系统进行查询.修改等操作后,该进程占用大量的CPU以及内存 ...

  4. [廖雪峰] Git 分支管理(1):创建与合并分支(HEAD、master、dev、指针)

    每次提交,Git 都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支.HEAD 严格来说不是指向提交,而是指向 mas ...

  5. CentOS内核定制

    版本号:1.0.1 作者:石硕 更新:2014-05-09 15:04:53 ============================================================ ...

  6. GDI+用PNG图片做半透明异型窗口

    {*******************************************************} {                                          ...

  7. iOS sqlite 使用事务操作数据库

    业务层代码: //将解析的更新人员数据批量同步到数据库 +(void)operateCompUsers:(NSMutableArray*)operateCompUsers { sqliteHelper ...

  8. EBS应用版本

    SELECT a.release_name FROM apps.fnd_product_groups a;

  9. SpringMVC方法传递集合数组

    背景:实体集合作为参数   数据准备: 1.实体类 class A {private int id; private String name; } 2.集合json字符串 [{"id&quo ...

  10. 关于面试总结8-http协议相关面试题

    前言 在PC浏览器的地址栏输入一串URL,然后按Enter键这个页面渲染出来,这个过程中都发生了什么事?这个是很多面试官喜欢问的一个问题 如果测试只是停留在表面上点点点,不知道背后的逻辑,是无法发现隐 ...