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. RSF 分布式 RPC 服务框架的分层设计

    RSF 是个什么东西? 一个高可用.高性能.轻量级的分布式服务框架.支持容灾.负载均衡.集群.一个典型的应用场景是,将同一个服务部署在多个Server上提供 request.response 消息通知 ...

  2. 设计模式学习之访问者模式(Visitor,行为型模式)(21)

    参考:https://www.cnblogs.com/edisonchou/p/7247990.html 在患者就医时,医生会根据病情开具处方单,很多医院都会存在以下这个流程:划价人员拿到处方单之后根 ...

  3. [CF662C] Binary Table(FWT)

    题意: https://www.cnblogs.com/cjyyb/p/9065801.html 题解:

  4. codeforces 493 div1 e

    题解: 和这件zhcs的那题有点像 第一种做法是考虑i,i+1之间的贡献 这样子就是矩形加减然后求矩形最小值个数 另一种做法是我们从左向右维护mx-nx-r+l 跟之前那题一样我们知道这个的最小值为0 ...

  5. Ubuntu+IntelliJ IDEA+Android 配置NDK环境+openCV

    最近需要将Python人证对比模型移植安卓端.安卓端需要使用openCV简单的人像提取处理.在配置openCV前首先需要配置NDK环境. NDK的介绍(http://www.cnblogs.com/l ...

  6. eclipse向tomcat部署站点发现没有class文件。

    其实大部分解决办法在网上都有的,例如这里: https://blog.csdn.net/shiyuehit/article/details/53262807 eclipse下无法自动编译或编译失败等问 ...

  7. eclipse下启动tomcat访问网址报404错误

    问题: 解决步骤 首先检查一遍自己tomcat的相关配置,如果无误,则继续下面的操作 1.打开server视图,双击tomcat进入编辑页面 选择其中的第二个选项,并且修改Server path中的内 ...

  8. java动态代理实现与原理详细分析

    关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式    代理模式是常用的java设计模式, ...

  9. 转:python request属性及方法说明

    转:http://blog.csdn.net/u013398398/article/details/52214582 if  request.REQUEST.has_key('键值'): HttpRe ...

  10. RHEL5.8安装

    创建完成后新的虚拟机.使用光盘启动后,启动界面如下图. 大概介绍下显示界面内容的意思:     1.To install or upgrade in graphical mde, press the ...