【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 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.       Given k will be between 1 and n! inclusive.

Example 1:

Input: n = 3, k = 3                Output: "213"

Example 2:

Input: n = 4, k = 9               Output: "2314"

思路

  看到这道题的时候我用的之前做的排列方法一样,就是一个一个的进行组合,当排列了第k个时直接结束排列,然后返回结果。但是提交时却是时间超时了。因为当n为9时,输出最后一个序列的运行时间特别长。这时我在想有没有什么其他的办法来解决这个问题,然后我当时想到了因为排列时是从第一个元素开始的,相当于每次固定一个元素,然后对后面的 n-1个元素进行排序。根据这个我们可以想到是不是可以根据n-1组合得数量来求得以第一个元素为准有多少种排列数。然后和k进行计算得到当前是对第几个元素为准得进行排列。然后直接以这个元素进行排列得到结果。这样可以省去前面元素排列时所耗费的时间。写出来也成功通过了。
  但是运行结果的效率还是比较低,我们可以在上面的方法继续改进,就是使用循环每次根据k的值都从1至n个元素中挑选出一个,直到k为0时,然后组合结果。得到最终的序列,也就是第K个序列。但是这种办法自己没能写出来。
解决代码(第一种思路)

 
 1 class Solution(object):
 2     def getPermutation(self,n, k):
 3         if n == 1:
 4             return '1'
 5         nums = [i for i in range(1, n + 1)]
 6         res = []
 7         dp = [1]* (n-1) 
 8         for i in range(2, len(dp)+1):        # 计算 n-1个元素有多少种组合数, 这里使用的数组来记录前一个元素的的组合数
 9             dp[i-1] = i*dp[i-2]
10         num = dp[-1]          
11         index = 0
12         if k > num:              # 根据k来判断当前是对第几个元素来进行排列。
13             index = k//num
14             if k % num == 0:     
15                 index -= 1
16             k -= num*index
17         path = [nums[index]]
18         nums.pop(index)
19         self.permutation(nums, path, res, [0], k)
20         return ''.join(str(i) for i in res[-1])
21 
22 
23     def permutation(self, nums, path, res, k, kth):      # 排列组合
24         if not nums:
25             res.append(path)
26             k[0] += 1
27             return
28         for i in range(len(nums)):
29             self.permutation(nums[:i] + nums[i + 1:], path + [nums[i]], res, k, kth)
30             if k[0] >= kth:      # 第K个排列时直接返回
31                 return

猜你喜欢

转载自www.cnblogs.com/GoodRnne/p/10746298.html
今日推荐