题目:

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,排列成n位数,会有n!种组合,按大小排列,输出第k个数的值。

代码:

该题目看起来就不是那么复杂,但是是medium的,说明把所有的数字排出来,排序,肯定是不行的。

于是,观察规律,肯定要先确定最高位的数字。1-n无论哪一个数字在最高位,都对应(n-1)!个组合的数字。

当k>(n-1)!且k<2*(n-1),说明第一位数字是2,因为1开头的排完了,也没有排到K,但也不会比两个(n-1)!大,所以首位可以确定。

当k<(n-1)!,自然首位就是剩余元素中最小的那个。比如一开始1-n,自然就是1了。

根据该规律,分情况,递归求出每次剩余元素中应该放在首位的那个,用链表记录1-n个元素,方便删除操作,首位用栈记录(方便):

java代码,不难理解,但还是试了半天,哎。。。:

//递归求阶乘
    public int factorial(int n) {
        if(n>1) {
            n = n*factorial(n-1);
        }
        return n;
    }
    //从首位开始,递归入栈每一位对应元素
    ArrayDeque<Integer> stack=new ArrayDeque<Integer>();
    public void getFirstNum(List<Integer> num,int k) {
        int i = 1;
        int n=num.size();
        int temp = factorial(n-1);
        //每次当n为1的时候,只有一个元素了,直接入栈并退出函数
        if(n==1) {
            stack.push((Integer) num.get(0));
            System.out.println("入栈: "+(Integer) num.get(0));
            return;
        }
        //k小于(n-1)!,所以直接取链表中最小的数为首位,入栈
        if(temp >=k) {           
            stack.push((Integer) num.get(0));
            System.out.println("入栈: "+(Integer) num.get(0));
            num.remove(0);
            getFirstNum(num,k);
        }
        else {
            //k大于(n-1)!,循环找出k大于几个(n-1)!
            while (i*temp < k){
              i++;
                  //k大于i个(n-1)!,取链表中第i个位置对应的数为首位,入栈
                if(i*temp >= k) {                    
                    stack.push((Integer) num.get(i-1));                    
                    System.out.println("入栈: "+(Integer) num.get(i-1));
                    num.remove(i-1);
                    k = k-(i-1)*factorial(n-1);
                    getFirstNum(num,k);
                    break;
                }                  
            }  
        }
    }
    //获得相应位置的排列
    public String getPermutation(int n, int k) {
       if(n==0){return null;}
       int result_int = 0;
       String result_str = null;
       ArrayList<Integer> num = new ArrayList<Integer>(n);
       for (int j=1;j<=n;j++) {
           num.add(j);
       }     
       getFirstNum(num,k);
       
       while(!stack.isEmpty()) {
           result_int= result_int*10+ stack.pollLast();
       }       
       System.out.println("第"+k+"元素是: "+result_int);
       result_str = String.valueOf(result_int);
       return result_str;               
    }

结果:

60. Permutation Sequence的更多相关文章

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

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

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

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

  5. 【LeetCode】60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

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

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

  7. LeetCode OJ 60. Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...

  8. 60. Permutation Sequence (String; Math)

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

  9. 60. Permutation Sequence(求全排列的第k个排列)

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

随机推荐

  1. PHP基础Mysql扩展库

    mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集     ddl:数据定义语句     dml:数据操作语句     dql:数据查询 ...

  2. 【bzoj3631】[JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树"上.松 ...

  3. WPF:依赖属性的应用

    依赖属性与一般属性相比,提供了对资源引用.样式.动画.数据绑定.属性值继承.元数据重载以及WPF设计器的继承支持功能的支持. 下面的这个Demo来自<葵花宝典--WPF自学手册>. 1.M ...

  4. mac 下 xampp 多域名 多站点 多虚拟主机 配置

    前言:最近用mac工作了,需要搭建个调试前段程序的站点,选了xampp,需求是能同时运行多个站点,多个域名,目录自定义,网上找了好多资料,都感觉有些不符合心意,且复制文确实很多,甚至有些没实践过的在乱 ...

  5. STL标准模板库介绍

    1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...

  6. 初识Flask

    首先在学习flask的前提,我是使用了很久的django和tornado,现在在写总结也是本着工作后方便使用flask 少点东西,对flask的介绍和优点总结 1.安装 pip install fla ...

  7. 2.3---删除链表的结点,不提供头结点(CC150)

    这里,注意如果是尾结点,那么无解. public class Solution { public void deleteNode(ListNode node) { //利用李代桃僵 // // if( ...

  8. saltstack/salt的state.sls的使用

    SLS(代表SaLt State文件)是Salt State系统的核心.SLS描述了系统的目标状态,由格式简单的数据构成.这经常被称作配置管理 首先,在master上面定义salt的主目录,默认是在/ ...

  9. 谷歌chrome浏览器和火狐firefox浏览器自带http抓包工具和请求模拟插件

    谷歌chrome浏览器自带http抓包工具 chrome://net-internals/ 谷歌chrome浏览器http请求模拟插件:postman 火狐http请求模拟插件:httprequest ...

  10. memcache的带图形界面监控工具memcachephp

    memcache也有一款图形界面的监控工具(memcachephp),可以通过这个工具查看到局域网内所有部署memcache机器或者端口的memcache的运行情况,对我们监控memcache的缓存命 ...