题目:

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. 不挣扎了,开始学习LINQ TO XML,进而来解析网页。

    找到了别人遇到和我一样的问题:http://ylad.codeplex.com/discussions/430095(英文) 一位叫做Mister Goodcat的提供了信息: Short answe ...

  2. objective-c-sort-keys-of-nsdictionary-based-on-dictionary-entries

    NSArray *keys = [someDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NS ...

  3. Mac os装软件时提示显示需要安装旧Java SE 6运行环境解决办法

    这个时Java版本的问题,换用合适的低版本即可,下面是官方的 https://support.apple.com/kb/DL1572?viewlocale=zh_CN&locale=en_US ...

  4. C++中的单例模式(转)

    单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,G ...

  5. java中二进制和流的相互转换

    流转二进制 public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream ou ...

  6. iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例

    一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...

  7. eclipse添加velocity项目

    1.首先添加jar包,记得包含以下的主要两个类别 2.新建一个servlet类(继承自VelocityViewServlet) package com.servlet; import java.uti ...

  8. buildroot 添加ssh,以及使用stftp 服务

    上一篇水了一下关于buildroot的基本操作,这一章水一下开启SSH服务以及配置sftp服务,以及静态IP的设置. 配置: make menuconfig Target packages  ---& ...

  9. 解决vista和win7在windows服务中交互桌面权限问题:穿透Session 0 隔离

        在某国外大型汽车公司BI项目中,有一个子项目,需要通过大屏幕展示销售报表,程序需要自动启动和关闭.开发人员在开发过程中,发现在Win7的service中不能直接操作UI进程,调查过程中,发现如 ...

  10. 报错:No package erlang available

    问题 yum install erlang 报错:No package erlang available 同样的,如果我们在安装nginx的时候出现"No package nginx ava ...