题目:

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. 关于Tchar

    因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包裹),这样对应的就有了两套字符串处理函数,比如:strlen和w ...

  2. 在PHP中$_SESSION的使用方法

    使用PHP应用session时,将session中的数据存储在服务器上,然后通过客户端传来的sessionID识别客户端的信息,并提取信息. php中的session的常用操作:session的写入. ...

  3. 【C语言入门教程】4.1 一维数组

    数组与指针涉及到数据在内存中的存储位置问题,数组由连续的存储单元组成,最低地址对应于数组的第一个单元,最高地址对应于数组的最后一个单元.指针是一种特殊的变量,该变量所存放的是内存地址,通过指针变量可访 ...

  4. Redis学习笔记十:独立功能之监视器

    通过执行 monitor 命令可以让客户端自己变成一个监视器,实时接收并打印当前处理的命令请求的相关信息. 127.0.0.1:6379> monitor OK 1451752646.83727 ...

  5. DEV控件Grid显示行号

    DEV控件Grid的显示行号需要通过一个事件来设置,具体设置代码为: private void gridView1_CustomDrawRowIndicator(object sender, DevE ...

  6. Java多线程基础知识(三)

    一. 管道输入/输出流 它和其它文件输入/输出流或网络输入/输出流的不同之处,它主要是线程之间的数据传输,而传输的媒介是内存. 管道输入/输出流主要包含四中实现: 1. PipedOutputStre ...

  7. iOS开发——UI基础-UIImage,UIImageView的使用

    1.UIImage 创建UIImage的两种方法 UIImage *image = [UIImage imageNamed:imageNmae]; UIImage *image = [UIImage ...

  8. UNTIY3D接入91SDK的办法

    原地址: http://bbs.18183.com/thread-111324-1-1.html UNITY3D接入Android-SDK 方法一:把UNITY3D游戏打成安卓项目文件,修改安卓项目文 ...

  9. Java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls).

  10. BZOJ 2124: 等差子序列

    Sol 线段树+Hash. 首先暴力 等差子序列至少3项就可以了,就枚举中项,枚举公差就可以了,只需要一个数在中项前出现,另一个数在中项前没出现过就可以了.复杂度 \(O(n^2)\) 然后我想了一个 ...