关闭

【Leetcode】Permutation Sequence

2336人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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.

思路:

从左往右计算,先计算最高位。 应该为k/(n-1)! ,下一位应该是(k%(n-1)!)/(n-2)!,以此类推。 

算法:

public String getPermutation(int n, int k) {  
        int nums[] = new int[n];  
        int pCount=1;  
        for(int i=0;i<nums.length;i++){  
            nums[i] = i+1;  
            pCount *= (i+1);//n!  
        }  
        k--;//下标从0开始  
        String res = "";  
        for(int i=0;i<n;i++){  
            pCount /=n-i;//(n-i-1)!  
              
            int r = k/pCount;  
            res+=nums[r];//第r小的元素  
            for(int j=r;j<n-1-i;j++){ //获取第r小的元素 所以将已经用过的元素覆盖    
                nums[j] = nums[j+1];  
            }  
            k = k%pCount;  
        }  
        return res;  
    }  


1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

[LeetCode]Permutation全排列和去重全排列

一、问题描述: 借助这道题总结一下全排列问题吧 https://leetcode.com/problems/permutations/ Given a collection of disti...
  • CristianoJason
  • CristianoJason
  • 2016-04-20 14:02
  • 1928

[C++]LeetCode: 114 Permutation Sequence(返回第k个阶乘序列——寻找数学规律)

题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling ...
  • cinderella_niu
  • cinderella_niu
  • 2015-01-20 20:48
  • 1007

Permutation Test 置换检验(转)

Permutation Test 置换检验 显著性检验通常可以告诉我们一个观测值是否是有效的,例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等(或者那个均值更大)。我们在实验中...
  • u011467621
  • u011467621
  • 2015-08-25 09:18
  • 3593

leetcode第一刷_Permutation Sequence

这道题还挺好的,如果你的思路是每次生成一个全排列,然后累计到k次,那么停下来吧,肯定超时了亲。。 微软今年的笔试题里有一道类似的,我之前已经提到过了,是只有0和1的字符串,求第k个排列是什么样子的。...
  • u012792219
  • u012792219
  • 2014-05-12 22:25
  • 1191

LeetCode_60---Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of th...
  • bingbing8219
  • bingbing8219
  • 2015-06-28 10:51
  • 271

[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...
  • u014654002
  • u014654002
  • 2016-05-22 19:47
  • 127

leetcode——60——Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the ...
  • happyxuma1991
  • happyxuma1991
  • 2016-04-17 13:43
  • 124

leetcode -- Permutation Sequence -- 重点

https://leetcode.com/problems/permutation-sequence/参考:http://bangbingsyb.blogspot.hk/2014/11/leetcod...
  • xyqzki
  • xyqzki
  • 2015-12-13 14:11
  • 264

leetcode | Permutation Sequence

Permutation Sequence : https://leetcode.com/problems/permutation-sequence/问题描述:The set [1,2,3,…,n] c...
  • quzhongxin
  • quzhongxin
  • 2015-05-30 15:39
  • 492

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
  • 220
    个人资料
    • 访问:556231次
    • 积分:8555
    • 等级:
    • 排名:第2655名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论