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 (ie, 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.

如果用前面2道题 的递归 非递归 都会超时。

只能用数学的方法计算。

 class Solution(object):

     def getPermutation(self, n, k):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
k = k -1
nums = list(range(1, n + 1))
res = ''
while len(nums)!=0:
ai =int(k/self.fn(n-1))
res += str(nums[ai])
del nums[ai]
k = k % self.fn(n-1)
n = n - 1
return res
def fn(self, n):
res = 1
while n != 0:
res = res * n
n = n - 1
return res

60. Permutation Sequence(求全排列的第k个排列)的更多相关文章

  1. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  2. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  3. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  4. 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 ...

  5. 60. Permutation Sequence

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

  6. 【LeetCode】60. Permutation Sequence

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

  7. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. LeetCode OJ 60. Permutation Sequence

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

  9. 60. Permutation Sequence (String; Math)

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

随机推荐

  1. C++ 抽象类一(多继承与赋值兼容性原则)

    //多继承与赋值兼容性原则 #include<iostream> using namespace std; class Point{ public: Point(){ a = ; b = ...

  2. C++ 类的多态四(虚析构函数的重要性)

    //虚析构函数的重要性 #include<iostream> using namespace std; /* 虚析构函数 主要用在多态中,用来释放子类对象内存空间,如果不使用虚析构函数, ...

  3. 在mybatis 中批量加载mapper.xml

    可以直接加载一个包文件名,将这个包里的所有*mapper.xml文件加载进来. 指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载: 必须按一定的标准:即xml文件和 ...

  4. 【ARDUINO】串口无法打开问题

    1.查看是否串口被锁 sudo arduino ls /var/lock sudo rm /var/lock/LCK..ttyACM* 2.查看arduino安装位置 dpkg -S XXXX 3.原 ...

  5. VSCode调试配置

    http://code.visualstudio.com/docs/editor/debugging#_launch-configurations VSCode内置Node.js运行时, 能调试jav ...

  6. Django admin 的 9 个技巧

    Tip 1:Django admin 后台不限于用 Django 开发的网站 虽然 Django admin 管理界面可以非常友好的用在 Django 项目的其它部分,它同样可以很容易用于其它像传统的 ...

  7. Python中xlrd和xlwt模块使用方法 (python对excel文件的操作)

    本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xlrd和xlwt模块 xlrd和xlwt模块不是 ...

  8. Linux下RPM包管理

    概述 一种用于互联网下载包的打包及安装工具,它包含在某些linux分发版中.它生成具有.RPM扩展名的文件.RPM是Redhat Package Manager(Redhat软件包管理工具)的缩写.这 ...

  9. 把資源加载到内存中 BMP 出错

    BMP文件放到VS的資源中時,VS會將BMP的文件頭去掉,即BITMAPFILEHEADER,這個結構體去除.所以當加載BMP到內存中時,如果是使用GDI+或是其它解釋庫時,會解析失敗. 所以在讀取B ...

  10. c++ 流继承关系