• 60. Permutation Sequence


    题目:

    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;               
        }

    结果:

  • 相关阅读:
    python内置函数
    conda和anaconda的区别
    闭包,装饰器,property
    【模板】大数乘法(51nod 1027)
    51nod 1791 合法括号子段
    51nod 1419 最小公倍数挑战
    51nod 1241 特殊的排序
    51nod 1090 3个数和为0
    【模板】51nod 1051 最大子矩阵和
    51nod 1267 4个数和为0
  • 原文地址:https://www.cnblogs.com/yuanzhaoyi/p/5868543.html
Copyright © 2020-2023  润新知