[leetcode] Permutation Sequnce

原题地址:https://oj.leetcode.com/submissions/detail/5341904/

题意:

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.

解题思路:刚开始用dfs,但一直TLE。貌似用java和c++写dfs可以过,看来python确实慢啊。只能采用一种更巧妙的思路了。

     

其实本题数据不大,n最多为9,9! = 362880,枚举应该能够通过(我没试验)。

我采用的方法是计算第k个Permutation。

假设n = 6,k = 400

先计算第一位,

第一位为6,那么它最少也是第5! * 5 + 1个排列,这是因为第一位为1/2/3/4/5时,都有5!个排列,因此第一位为6时,至少是第5! * 5 + 1个排列(这个排列为612345)。

5! * 5 + 1 = 601 > k,所以第一位不可能是6.

一个一个地枚举,直到第一位为4时才行,这时,4xxxxx至少为第5! * 3 + 1 = 361个排列。

然后计算第二位,

与计算第一位时的区别在于,46xxxx至少为第4! * 4 + 1 = 97个排列,这是因为比6小的只有5/3/2/1了。

最后可以计算出第二位为2。

最终得出第400个排列为425361。

代码:

class Solution:
    # @return a string
    def getPermutation(self, n, k):
        #http://www.cnblogs.com/zuoyuan/p/3785530.html
        res = ‘‘
        fac = 1
        k -= 1
        for i in range(1,n): fac *= i
        num = [i for i in range(1,10)]
        for i in reversed(range(n)):
            curr = num[k/fac]
            res += str(curr)
            num.remove(curr)
            if i != 0:
                k   %= fac
                fac /= i
        return res

参考:

http://www.cnblogs.com/zuoyuan/p/3785530.html

时间: 2024-10-08 11:13:08

[leetcode] Permutation Sequnce的相关文章

LeetCode: Permutation Sequcence 题解

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" "3

Leetcode permutation 系列

关于permutation的讲解,请参见http://blog.csdn.net/xuqingict/article/details/24840183 下列题目的讲解均是基于上面的文章: 题1: Next Permutation Total Accepted: 8066 Total Submissions: 32493My Submissions Implement next permutation, which rearranges numbers into the lexicographic

LeetCode: Permutation Sequence [059]

[题目] 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" &

[leetcode]Permutation Sequence @ Python

原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: 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&

leetcode permutation/combination

Next Permutation 将整个排列看成是一个数,按数大小排列,求下一个排列 // ①从右到左找到第一个非递增的位置 pivot // ②从右到左找到第一个大于 *pivot 的位置 change  // ③交换*pivot与*change // ④将pivot右边的元素全部逆置 // @algorithm http://?sherlei.blogspot.com/2012/12/leetcode-next-permutation.html  // @author soulmachine

LeetCode: Permutation Sequence 解题报告

Permutation Sequence https://oj.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): "123&

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): "123" "132" "213" "231" "312" "

[leetcode]Permutation Sequence

问题描述: The set [1,2,3,-,n] contains a total ofn! 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" &

[LeetCode] Permutation in String

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string. Example 1: Input:s1 = "ab" s2 = "eidbaooo"