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个序列。但是这种办法自己没能写出来。
解决代码(第一种思路)


 class Solution(object):
def getPermutation(self,n, k):
if n == 1:
return ''
nums = [i for i in range(1, n + 1)]
res = []
dp = [1]* (n-1)
for i in range(2, len(dp)+1): # 计算 n-1个元素有多少种组合数, 这里使用的数组来记录前一个元素的的组合数
dp[i-1] = i*dp[i-2]
num = dp[-1]
index = 0
if k > num: # 根据k来判断当前是对第几个元素来进行排列。
index = k//num
if k % num == 0:
index -= 1
k -= num*index
path = [nums[index]]
nums.pop(index)
self.permutation(nums, path, res, [0], k)
return ''.join(str(i) for i in res[-1]) def permutation(self, nums, path, res, k, kth): # 排列组合
if not nums:
res.append(path)
k[0] += 1
return
for i in range(len(nums)):
self.permutation(nums[:i] + nums[i + 1:], path + [nums[i]], res, k, kth)
if k[0] >= kth: # 第K个排列时直接返回
return

【LeetCode每天一题】Permutation Sequence(排列序列)的更多相关文章

  1. 060 Permutation Sequence 排列序列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,可得到如下序列 (例如,  n = 3):   1."123"   2. & ...

  2. LeetCode(60) Permutation Sequence

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

  3. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  4. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

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

  6. [LeetCode] Permutation Sequence 序列排序

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

  7. 【leetcode刷题笔记】Permutation Sequence

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

  8. [LeetCode] 60. Permutation Sequence 序列排序

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

  9. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

随机推荐

  1. Vue中的template标签的使用和在template标签上使用v-for

    我们知道  .vue 文件的基本结构是: <template> ........ </template> <script> export default { nam ...

  2. 倒影问题(reflect:below)

    这个例子灵感来源于实现一个登录框下方的倒影: .box { width: 300px; height: 200px; border: 1px solid #1f637b; -webkit-box-re ...

  3. hexo基本操作

    1.新建一篇文章:hexo new post "article title" 2.生成静态网页:hexo g 3.预览效果:hexo s 4.发布:hexo d

  4. 14.并发与异步 - 3.C#5.0的异步函数 -《果壳中的c#》

    14.5.2 编写异步函数 private static readonly Stopwatch Watch = new Stopwatch(); static void Main(string[] a ...

  5. [慢更]Sublime Text 快捷键及使用过的插件

    整理自己常用的sublime text命令和插件 1.pretty json Json 快速格式化,免去url访问json站点的烦恼. 摘自:https://segmentfault.com/a/11 ...

  6. Codeforces Round #524 (Div. 2) F

    题解: 首先这个东西因为强制在线区间查询 所以外面得套线段树了 然后考虑几条线段怎么判定 我们只需要按照右端点排序,然后查询的时候查找最右节点的前缀最大值就可以了 然后怎么合并子区间信息呢 (刚开始我 ...

  7. UIWebView的常用方法

    //webview导航栏类型enum UIWebViewNavigationType : Int { case LinkClicked case FormSubmitted case BackForw ...

  8. [NewLife.XCode]高级增删改

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...

  9. 构建Maven父子工程

    IDEA构建maven父子工程:  1.打开IDEA,Create New Project 如图:   如果没有弹出新建界面,可以先 file-->Close Project 如图: 2.创建父 ...

  10. Web版记账本开发记录(一)代码和功能展示

    一丶基本机构 数据库截图 record表 年份表 index.jsp <%@ page language="java" contentType="text/html ...