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

最新文章

  1. 在Winform界面中实现对多文档窗体的参数传值
  2. js 模块化历程
  3. PHP 小方法之 写日志方法
  4. 剑指offer题目51-60
  5. Cannot export AX project in AX7
  6. 12-1 上午mysql 基本语句
  7. 使用BusyBox制作Linux根文件系统
  8. 树莓派实现TimeMachine以及samba服务
  9. Http请求通信(工具类)
  10. 高频dom操作和页面性能优化(转载)
  11. 微信小程序——微信支付
  12. 移动App,AJAX异步请求,实现简单的增、删、改、查
  13. SQL知识点
  14. java项目发布到linux服务器,tomcat正常启动但没加载项目
  15. maven install 错误
  16. Django 分页器
  17. ArcGIS AddIN Sample学习笔记
  18. spring boot 2.0 源码分析(一)
  19. Python基础教程学习笔记:第一章 基础知识
  20. JDBC核心API

热门文章

  1. linux在桌面和dos之间的切换
  2. JS 怎么刷新当前页面
  3. Temporal Ensembling for Semi-Supervised Learning
  4. Apollo 刨析:Localization
  5. 【vijos】1629 八(容斥原理+dfs)
  6. 【JavaEE】SSH+Spring Security整合及example
  7. PowerDesigner使用教程3
  8. golang build 编译规则
  9. 【BZOJ3166】[Heoi2013]Alo 可持久化Trie树+set
  10. 纯css实现隐藏滚动条仍可以滚动