60. Permutation Sequence(求全排列的第k个排列)
[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):
"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.
如果用前面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个排列)的更多相关文章
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 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 ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 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 ...
- 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 ...
随机推荐
- transform使用参考指南
transform使用参考指南 2010-09-18 16:06:33| 分类: html5+css3|字号 订阅 语法 transform :none | <transform-fu ...
- 【vijos】1781 同余方程(拓展欧几里得)
https://vijos.org/p/1781 学习了下拓欧.. 求exgcd时,因为 a*x1+b*y1=a*x2+b*y2=b*x2+(a-b*[a/b])*y2 然后移项得 a*x1+b*y1 ...
- vi 的使用,很详细
来源:鸟哥的Linux私房菜 网址:http://vbird.dic.ksu.edu.tw/linux_basic/0310vi_2.php
- Duilib教程-非DUI控件
DUILIB并不是真正的DUI,至少有部分控件不是完全DUI的.其实包括: 1.EDIT. 它的实现原理是,CEditUI包含一个窗口CEditWnd,流程如下: 1)鼠标单击,创建窗口见 EditU ...
- 分布式服务框架dubbo入门实例
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- Kotlin——初级篇(八):关于字符串(String)常用操作汇总
在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin--初级篇(三):数据类型详解这篇文章. 在 ...
- HDU 3572(Task Schedule) ISAP做法
题目链接:传送门 题目大意:有n个任务,m个机器.每个机器最早在 Si 天开始,最晚在 Ei 天结束,需要 Pi 天完成.可以中途换机器做,也可以中途打断不做,过后再做 只要在规定时间内都行.每个 ...
- nodejs 循环中操作需要同步执行解决方案
最近用nodejs做了个针对某网站的小爬虫.干坏事得低调对吧,不能同时开太多的网络访问,结果各种回调/循环虐的心力交瘁. 经过了n次的百度\哥哥后终于拼出了自己要的功能.不敢独享分享出来以供大家参考. ...
- ubuntu 安装低版本firefox
firefox 57以后很多插件不支持了,ubuntu16自带火狐版本59,想换回56. 1.下载想换回的版本 https://ftp.mozilla.org/pub/mozilla.org/fire ...
- Learning PHP Design Patterns
Learning PHP Design Patterns CHAPTER 1 Algorithms handle speed of operations, and design patterns ha ...