题目:

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面向对象(OOP)编程入门教程】4.如何抽象出一个类?

    上面已经介绍过了, 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易,只要掌握基本的程序语法定义规则就可以做的出来,那么难点在那里呢 ...

  2. jQuery的常用函数扩展

    (function ($) { /**************************获得URL的参数************************************/ //参数:URL中的参 ...

  3. Android ListView 图片异步加载和图片内存缓存

    开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...

  4. JAVA Io 缓冲输入输出流

    java中提供带缓冲的输入输出流.在打开文件进行写入或读取操作时,都会加上缓冲,提高了IO读写性能. 1. BufferedInputStream 缓冲输入流 2. BufferedOutputStr ...

  5. python 数据结构 初学时没太注意却发现很有用的点点滴滴

    1. list.extend(L) 将指定列表中的所有元素附加到另一个列表的末尾:相当于a[len(a):] = L. 2. list.pop([i]) 删除列表中指定位置的元素并返回它.如果未指定索 ...

  6. 跟着百度学PHP[4]OOP面对对象编程-14-克隆对象__clone()方法

    $b=clone ($a) #克隆a对象. <?php class Human { private $name; private $sex; private $age; function __c ...

  7. pro*c添加SQLCHECK后编译报错PLS-S-00201

    如果在pro*c中调用数据库了里的函数,就需要在proc的cfg配置文件中添加一行: SQLCHECK=SEMANTICS 但是添加之后又会出现PLS-S-00201错误,原因在与添加SQLCHECK ...

  8. Android权限 uses-permission

    Manifest.permission 官方API说明: http://developer.android.com/reference/android/Manifest.permission.html ...

  9. tfw格式图解

    话不多说,直接看图. 上图中的UV坐标,实际上只的是图像的 横向坐标 和 纵向坐标 .即图像的行和列坐标. 对于图上任意一个像素点(col,row)这个坐标,换算其地理坐标就十分简单. GeoX = ...

  10. Python自动化之select解析

    select原理 网络通信被Unix系统抽象为文件的读写,通常是一个设备,由设备驱动程序提供,驱动可以知道自身的数据是否可用.支持阻塞操作的设备驱动通常会实现一组自身的等待队列,如读/写等待队列用于支 ...