`

LeetCode - 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.

If this is a follow-up question of "next permutation" (see previous post), the first thought will be calling nextPermutation k times. That's expensive: we have to compute all previous k-1permutations even though we never gonna use them!

Take a look at the entire sequence.
The first number will not change to next until all permutations of the rest of n-1 numbers have shown up. That said, given n = 4, k = 15 (= 2 * 3! + 1 * 2! + 1 * 1! + 0), the first number will be 3 (the 3-rd of [1,2,3,4]), the second number will be 2 (the 2-nd of [1,2,4]), and the last two numbers will be 1 and 4 (the 1-st permutation of [1,4]).

By doing these math, we only need O(n) time to get an index for the current spot (O(n) time to compute n!).
Notice that the calculated index is a relative index and thus we need to shift the numbers after we find out the one for the current spot (or erase the current one if we use separate strings for original and k-th permutation).

So, the total running time is O(n^2) since we may need to shift O(n) times. Also, it takes extra O(n) space for the factorials to reduce redundant calculations.

 

  1. 找没用过的里面的第(k – 1) / (n – 1)!个数字,放第一位上。
  2. k = (k – 1) % (n – 1)!,第一个数字确定了,剩下这些里面重新找第k个。
  3. n每次-1,比如第一位确定后有(n-1)!种后面数字的排法,第二位也确定了后面的数字排法就又少了一位(n – 1 – 1)!
public String getPermutation(int n, int k) {
    List<Integer> numberList = new ArrayList<Integer>();
    int[] fact = new int[n+1];
    fact[0] = 1;
	for(int i=1; i<=n; i++) {
		numberList.add(i);
		fact[i] = fact[i-1]*i;
	} 
    // change K from (1,n) to (0, n-1) to accord to index  
    k--;  
    String result = "";  
    for(int i=n-1; i>=0; i--)  {    
         int choosed = k / fact[i];
         k -= choosed*fact[i];
         // restruct numberList since one number has been picked  
         result += numberList.remove(choosed); 
    }  
    return result;  
}

 

Reference:

http://n00tc0d3r.blogspot.com/2013/05/permutation-sequence.html

http://yucoding.blogspot.com/2013/04/leetcode-question-68-permutation.html

http://www.programcreek.com/2013/02/leetcode-permutation-sequence-java/

http://leetcodenotes.wordpress.com/2013/10/20/leetcode-permutation-sequence/

分享到:
评论

相关推荐

    js-leetcode题解之60-permutation-sequence.js

    js js_leetcode题解之60-permutation-sequence.js

    java-leetcode题解之Permutation Sequence.java

    java java_leetcode题解之Permutation Sequence.java

    leetcode-cpp刷题

    - **2.1.13 Permutation Sequence** - 给定数字集合,返回第n个排列。 - 实现思路:利用数学方法计算出每一位上的数字。 - **2.1.14 Valid Sudoku** - 判断给定的数独是否有效。 - 实现思路:分别检查行、列和...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) !... Up to date (2016-12-18), there are `447` Algorithms / `13` ...31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| ...

    lrucacheleetcode-LeetCode:这个库用于总结leetcode中遇到的习题,期望按照数据结构和常用方法分成2类,进行总结,

    leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...

    leetcode代码200题c++

    1. **Permutation Sequence**:这个问题涉及到排列组合和字符串操作。它要求生成一个给定整数n的所有可能排列的序列。这通常需要对数字进行位操作或使用回溯法。 2. **First Missing Positive**:这是一个关于数组...

    c++-c++编程基础之leetcode题解第60题排列序列.zip

    LeetCode的第60题,也称为"排列序列"(Permutation Sequence),要求我们给定一个整数n,生成所有可能的n个数字的排列,并按照字典序排序。例如,当n=3时,排列序列应该是"123", "132", "213", "231", "312", "321...

    Coding Interview In Java

    236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination Sum 577 239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number ...

    leetcode_basic_60

    这个“leetcode_basic_60”主题可能聚焦于LeetCode上的第60题,即“Permutation Sequence”(排列序列)。这个问题涉及到组合数学、递归和字符串处理,是Python编程者常见的练习题目。 【描述】"leetcode_basic_60...

    javalruleetcode-SDE-Problems:标准SDE问题列表

    leetcode SDE-问题 标准 SDE 问题列表 第一天:(数组) 日 问题陈述 解决方案 困难 使用的数据结构 使用的算法 时间复杂度 空间复杂度 补充阅读 在 N 个整数的数组中查找重复项 中等的 大批 不适用 上) O(1) 在不...

Global site tag (gtag.js) - Google Analytics