题目

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. forof循环

    一.语法 1.遍历数组 let myArr=[1,2,3,4,5]; for (let ele of myArr) { console.log(ele); } let myArr=[1,2,3,4,5 ...

  2. 关于js操作符需要注意的地方

    本文仅仅介绍部分js操作符在实际应用中需要注意的地方. 布尔操作符: //1.逻辑与操作属于短路操作,即如果第一个操作数能够决定结果那么就不会再对第二个操作数求值 var found=true; va ...

  3. j.u.c系列(02)---线程池ThreadPoolExecutor---tomcat实现策略

    写在前面 本文是以同tomcat 7.0.57. jdk版本1.7.0_80为例. 线程池在tomcat中的创建实现为: public abstract class AbstractEndpoint& ...

  4. Azure虚机磁盘容量警报(邮件提醒)

    上周有个客户提出这样的需求:根据虚拟机磁盘的实际使用量,当达到某一阈值时设置邮件提醒. 在这个需求中我们只需要解决两点问题: 计算虚拟机磁盘实际使用量 发送邮件 使用VS新建一个名为Calculate ...

  5. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. ARM JTAG 调试原理

    ARM JTAG 调试原理 JTAG的接口是一种特殊的4/5个接脚接口连到芯片上 ,所以在电路版上的很多芯片可以将他们的JTAG接脚 通过Daisy Chain的方式连在一起,并且Probe只需连接到 ...

  7. 《Go学习笔记 . 雨痕》类型

    一.基本类型 清晰完备的预定义基础类型,使得开发跨平台应用时无须过多考虑符合和长度差异. 类型 长度 默认值 说明 bool 1 false   byte 1 0 uint8 int, uint 4, ...

  8. javascript 编辑网页

    javascript:document.body.contentEditable='true';document.designMode='on'; void 0 出处:http://zhidao.ba ...

  9. .NET轻量级ORM组件Dapper修炼手册

    一.摘要 1.1.为什么叫本次的分享课叫<修炼手册>? 阿笨希望本次的分享课中涉及覆盖的一些小技巧.小技能给您带来一些帮助.希望您在日后工作中把它作为一本实际技能手册进行储备,以备不时之需 ...

  10. 关于在Struts2的Action中使用domain模型接收参数的问题

    最近在搭建一个最新的ssh2框架,今天在调试的时候,发现了一个以前一直没有注意过的问题,我在Action中使用域模型的方式去接收jsp画面中的参数的时候,发现参数总是接收不完,头一次遇到这种问题,现在 ...