题目:给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
    "123"
    "132"
    "213"
    "231"
    "312"
    "321"
给定 n 和 k,返回第 k 个排列。
说明:给定 n 的范围是 [1, 9]。给定 k 的范围是[1,  n!]。

来源: https://leetcode-cn.com/problems/permutation-sequence/

法一:自己的代码    耗时很短,利用python自带的阶乘函数计算耗时会更短

思路:转化为一个纯数学的问题,关键是要把每种情况都考虑到,特别是除n!后是整数的情况,举例的时候就要把每种情况都枚举到,编程才不会出错

import math
class Solution:
def getPermutation(self, n: int, k: int):
# 自定义阶乘函数
def n_factorial(x):
if x == 0:
return 1
else:
return x * n_factorial(x-1)
nums = [i+1 for i in range(n)]
result = ''
while n > 0:
n = n - 1
# 通过观察数据可以看出,假如输入是(4,9),则说明以1开头的有3!个,以2开头的有3!个,
# 所以用9除以3的阶乘是1.5,1,5向上取整为2,即nums中的第二个数2是结果中的第一个数,
# 再用9减去6为3表示从2开始的组别中找第三个数,
res = k / n_factorial(n)
res_up = math.ceil(res)
# 注意这里向下取整必须是向上取整后减1,这是因为比如输入的是(4,6),则6除3!为1,1-1=0,所以不可直接向下取整
res_down = res_up - 1
k = k - res_down * n_factorial(n)
result = result + str(nums[res_up-1])
del nums[res_up-1]
print('-'* 20)
print('k', k)
# print(res)
print(res_up)
# print(res_down)
return result
if __name__ == "__main__":
duixiang = Solution()
a = duixiang.getPermutation(4,9)
print(a)

法二:自己的代码    利用回溯,但是超时,超时是因为没有用continue,每个分支都要检查一遍

class Solution:
def getPermutation(self, n: int, k: int):
nums = [i+1 for i in range(n)]
global count,result
count = 0
def backtrack(a='', nums=nums, ):
global count,result
# print('k',count)
if count == k:
return
if len(a) == n:
count += 1
# print('count', count)
if count == k:
result = a
# exit()
# return count
for i,j in enumerate(nums):
r = nums.copy()
del r[i]
# print('ttt',count)
# sign,result = backtrack(a+str(j), r)
backtrack(a+str(j), r)
backtrack()
return result

改进后的不会超时,注意这里return的用法,非常巧妙

class Solution:
def getPermutation(self, n: int, k: int):
nums = [i+1 for i in range(n)]
from1to9_factorial = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
def backtrack(a='', nums=nums,k = k ):
# 一旦满足条件立即逐层返回a,结束所有函数
if len(a) == n:
return a
t = from1to9_factorial[len(nums) - 1]
for i,j in enumerate(nums):
if k > t:
# 执行剪枝操作,如果大于t了,就结束本次循环
k = k - t
continue
# r中是下次for循环要遍历的数
r = nums.copy()
del r[i]
# 这里必须用return,避免了定义全局变量来解决问题
return backtrack(a+str(j), r, k)
return backtrack()
if __name__ == "__main__":
duixiang = Solution()
a = duixiang.getPermutation(4,10)
print(a)

60第K个排列的更多相关文章

  1. Java实现 LeetCode 60 第k个排列

    60. 第k个排列 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &q ...

  2. LeetCode 60 第K个排列

    题目: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "13 ...

  3. LeetCode 60. 第k个排列(Permutation Sequence)

    题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "1 ...

  4. 力扣60——第k个排列

    原题 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: 1. "123" 2. &qu ...

  5. 算法:60.第k个排列

    解答参考:https://blog.csdn.net/lqcsp/article/details/23322951 题目链接:https://leetcode-cn.com/problems/perm ...

  6. 代码题(45)— 下一个排列、第k个排列

    1.31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只 ...

  7. LeetCode:第K个排列【60】

    LeetCode:第K个排列[60] 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: &quo ...

  8. LeetCode(60): 第k个排列

    Medium! 题目描述: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" ...

  9. LeetCode 中级 - 第k个排列(60)

    可以用数学的方法来解, 因为数字都是从1开始的连续自然数, 排列出现的次序可以推 算出来, 对于n=4, k=15 找到k=15排列的过程: 1 + 对2,3,4的全排列 (3!个) 2 + 对1,3 ...

随机推荐

  1. Could not determine which “make” command to run. Check the “make” step in the build configuration

    环境: QT5.10 VisualStudio2015 错误1: Could not determine which “make” command to run. Check the “make” s ...

  2. 自学Python5.7-面向对象三大基本特征_封装

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  3. 【TJOI 2019】唱、跳、rap和篮球

    题意 有 $a$ 个 $0$,$b$ 个 $1$,$c$ 个 $2$,$d$ 个 $3$,求有多少种长度为 $n$ 且不包含 $0123$ 这个子串的字符串个数. $n\le 1000,\space ...

  4. tensorflow各版本下载地址

    https://pypi.org/project/tensorflow-gpu/1.13.0/#files 把13改对你想要的版本

  5. 阿里域名 ssl tomcat

    1.首先注册一个域名 2.添加一个信息模板(域名服务里边) 3.域名解析(默认解析127.0.0.1)  可以ping 域名试下看是否解析了(阿里有参考视频) 4.ssl 证书   免费版,网上有教程 ...

  6. 如何查看fullGC 次数

    如何查看fullGC 次数 如何较少fullGC 如何保证几周才发生一次fullGC

  7. vue自定义指令,自动调用下载的方法

    directives: { clickDown: { inserted (el, binding, item) { if (+binding.value.item.fromId === +item.c ...

  8. Nowcoder Monotonic Matrix ( Lindström–Gessel–Viennot lemma 定理 )

    题目链接 题意 : 在一个 n * m 的矩阵中放置 {0, 1, 2} 这三个数字.要求 每个元素 A(i, j) <= A(i+1, j) && A(i, j) <= ...

  9. POJ 1149 猪圈买猪 建图太强大!! 没有透彻领悟 慢慢消化

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19575   Accepted: 8948 Description ...

  10. [JZOJ6244]【NOI2019模拟2019.7.1】islands【计数】【图论】

    Description n<=1e9,M,K<=100 Solution 显然任选m个港口的答案是一样的,乘个组合数即可. 考虑枚举m个港口的度数之和D 可以DP计算 记\(F_{m,D} ...