Middle-题目93:60. Permutation Sequence

原创 2016年05月31日 19:42:39

题目原文:
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):

“123”
“132”
“213”
“231”
“312”
“321”
Given n and k, return the kth permutation sequence.
题目大意:
给出n和k,求出n的全排列中字典序第k位的排列。
题目分析:
观察3个数的全排列,可见最高位分别是1,1,2,2,3,3,那么可以分为3组,第1组最高位为1,第二组最高位为2,第三组最高位为3,每一组内又分别是剩下两个数的按字典序的全排列,因此可以递归的缩小问题规模。
构造一个动态数组list,初始化为[1,2,…,n],并把n!个数分为n组,算出k在第几组及组内序号,按组号对应从list中取出这一位,然后更新k为组内的序号,直到list为空则找到答案。文字描述可能还是有些抽象,接下来举例说明。
例:n=4,k=15.
Step1.首先初始化动态数组list=[1,2,3,4],4个数的全排列有4!=24种,分为4组,则15在第2组种,组内序号为3(组号和组内序号都从0开始)则取出list的2号元素’3’加入答案中,并更新n=3,k=3.
Step2.list=[1,2,4],3个数的全排列有6种,分为3组,则3在第1组,组内序号为1.取出list的1号元素’2’,并更新n=2,k=1
Step3.list=[1,4],2个数的全排列有2种,分为2组,则1在第1组,组内序号为0,取出list的1号元素’4’,并更新n=1,k=0
Step4.list=[1],1个数的排列只有1种,加入第0号元素’1’即可。
最后得到答案”3241”.
源码:(language:java)

public class Solution {
    private String current = "";
    public String getPermutation(int n, int k) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 1;i<=n;i++)
            list.add(i);
        backtrack(list, n, k);
        return current;
    }
    private void backtrack(List<Integer> list, int n, int k) {
        if(list.isEmpty())
            return;
        else {
            int groups = factorial(n-1);
            int groupid = (k-1)/groups;
            current+=String.valueOf(list.get(groupid));
            list.remove(groupid);
            backtrack(list, n-1, k-groupid*groups);
        }
    }
    public int factorial(int num){
        int sum=1;
        for(int i=1;i<=num;i++){
            sum *= i;
        }
        return sum;
    }
}

成绩:
4ms,beats 5.39%,众数3ms,41.65%
Cmershen的碎碎念:
似乎本题可以用之前求排列数里面的next_permutation实现。还有就是本题的n!是现场算的,如果打表算出,应该还可以快1ms.

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode 93 Permutation Sequence

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

LeetCode_60---Permutation Sequence

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

[leetcode] 【数组】 60. Permutation Sequence

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

leetcode——60——Permutation Sequence

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

leetcode60-Permutation Sequence(求指定位置的排列)

问题描述:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of t...
  • will130
  • will130
  • 2016年04月29日 21:19
  • 203

LeetCode(60): Permutation Sequence (C++)

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

LeetCode60——Permutation Sequence

LeetCode60——Permutation Sequence 又是全排列的问题。 循环调用next_permutation()这个方法测试过超时。 这个题参考了:http://blog.cs...

(Java)LeetCode-60. Permutation Sequence

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

60. Permutation Sequence -Medium

Question The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and label...

60. Permutation Sequence\78. Subsets\77. Combinations

Permutation Sequence 题目描述 代码实现 Subsets 题目描述 代码实现 Combinations 题目描述 代码实现60. Permutation Sequence题目描述T...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目93:60. Permutation Sequence
举报原因:
原因补充:

(最多只允许输入30个字)