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 ...
随机推荐
- windows下GVIM的配置(vimrc)
学习python时想要在gvim中配置python的编译环境,网上找到一个比较好用的vimrc配置,保存下来以备下次有需要. set encoding=utf-8 set termencoding=u ...
- css3兼容代码
1. 渐变:.gradient{ width:300px; height:150px; filter:alpha(opacity=100 finishopacity=50 style=1 startx ...
- XMLRPC 学习笔记(一)- Python 实现
参考文章: http://baike.baidu.com/view/643379.htm http://docs.python.org/2/library/xmlrpclib.html http:// ...
- WPF 附加属性的用法 (一)
public class MDCTest { public static DependencyProperty MouseDoubleClickCommandProperty = Dependency ...
- window 实用操作(结束已打开无法删除进程 内存占用)
1.win7删除文件,文件夹或文件已在另一程序中打开:https://jingyan.baidu.com/article/e75057f2a41e88ebc91a8985.html 删除文件时,提示“ ...
- Cocos2d-x 3.3Bate0 ExpandedListView
之前写的ExpandedListView版本号因为版本号升级这里提供Cocos2d-x 3.3Bate0 版本号 代码下载:http://download.csdn.net/detail/qqmcy/ ...
- Android 7.1.1 锁屏界面启动流程
前几天遇到一个低概率复现锁屏界面不显示,仅仅显示状态栏的问题,跟了下锁屏界面启动显示的流程,在这分享下,也方便以后自己查看.前面简介了下Zygote启动流程, Zygote进程启动后会首先创建一个Sy ...
- AndroidManifest.xml文件详解(activity)(五)
android:taskAffinity 这个属性用于跟Activity有亲缘关系的任务.带有相同亲缘关系的Activity,在概念上是属于相同任务的(从用户的角度看,它们是属于同一应用程序的).任务 ...
- NHibernate 1.0 Released 版本发布了
NHibernate is a port of Hibernate to the .NET platform. Hibernate is the leading open-source object- ...
- 项目分析(GS,NET,NGP关系)
看了两天,这三者之间的关系好像是这样的,因为GS和net在同一台机器上,所以用共享内存通信,毕竟共享内存通信是最快的进程间通信的方式,而NGP是属于客户端的,Net是属于服务器的,他与Net进程是基于 ...