【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:
"123""132""213""231""312""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(排列序列)的更多相关文章
- 060 Permutation Sequence 排列序列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,可得到如下序列 (例如, n = 3): 1."123" 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 ...
- 【LeetCode每天一题】Permutations(排列组合)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- LeetCode31 Next Permutation and LeetCode60 Permutation Sequence
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 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 ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode刷题笔记】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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 ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
随机推荐
- spring boot vuejs
项目介绍 Spring Boot 2.0 + VueJs 2.0 开发项目示例 GitHub: https://github.com/fqybzhangji/spring-boot-vue 码云: h ...
- shaderlab UV动画所需的变量声明
优化资源.美术需要迫使自己的顶点shader能够进行TRANSFORM_TEX运算,进行该运算的前提是需要声明一个 _MainTex_ST 变量,类型为float4即可. 此时就可以使用unity c ...
- 代码生成工具更新--快速生成Winform框架的界面项目
在之前版本的代码生成工具Database2Sharp中,由于代码生成都是考虑Winform和Web通用的目的,因此Winform界面或者Web界面都是单独生成的,在工具中生成相应的界面后,复制到项目里 ...
- 如何使用JMeter开源性能测试工具来构建Web性能测试体系
一.性能测试一些概念 性能测试:就是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 负载测试:确定在各种工作负载 ...
- win10安装Oracle11g-出现INS-13001环境不满足最低要求问题
今天安装Oracle11g,出现INS-13001环境不满足最低要求问题: 解决方法 在安装时点击setup.exe之后,出现了:[INS-13001]环境不满足最低要求 这时,打开你的解压后的dat ...
- Linux 添加到环境变量
在Linux下使用源码安装软件的时候,通常只能在软件安装目录下使用该软件命令,这样太麻烦,我们希望全局使用,可以将软件安装路径添加到系统环境变量里. 添加环境变量有2种方法: 1. 使用export命 ...
- SpringBoot启动tomcat源码解读
一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...
- JavaScript DOM事件模型
早期由于浏览器厂商对于浏览器市场的争夺,各家浏览器厂商对同一功能的JavaScript的实现都不进相同,本节内容介绍JavaScript的DOM事件模型及事件处理程序的分类. 1.DOM事件模型.DO ...
- flask微电影系统开发中上下文处理器
在做日志模块开发的过程中,我们遇到了一个问题,就是日期的问题,我们这个时间就要用到上下文处理器来解决问题,他的作用就是可以在我们固定模块中进行传递,我们的模块如下 @admin.context_pro ...
- (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等
Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...