[leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/
题意:
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):
"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.
解题思路:刚开始用dfs,但一直TLE。貌似用java和c++写dfs可以过,看来python确实慢啊。只能采用一种更巧妙的思路了。
其实本题数据不大,n最多为9,9! = 362880,枚举应该能够通过(我没试验)。
我采用的方法是计算第k个Permutation。
假设n = 6,k = 400
先计算第一位,
第一位为6,那么它最少也是第5! * 5 + 1个排列,这是因为第一位为1/2/3/4/5时,都有5!个排列,因此第一位为6时,至少是第5! * 5 + 1个排列(这个排列为612345)。
5! * 5 + 1 = 601 > k,所以第一位不可能是6.
一个一个地枚举,直到第一位为4时才行,这时,4xxxxx至少为第5! * 3 + 1 = 361个排列。
然后计算第二位,
与计算第一位时的区别在于,46xxxx至少为第4! * 4 + 1 = 97个排列,这是因为比6小的只有5/3/2/1了。
最后可以计算出第二位为2。
最终得出第400个排列为425361。
代码:
class Solution:
# @return a string
# def dfs(self, n, k, string, stringlist):
# if len(stringlist) == n:
# Solution.count += 1
# if Solution.count == k:
# print stringlist
# return
# for i in range(len(string)):
# self.dfs(n, k, string[:i]+string[i+1:], stringlist+string[i]) # def getPermutation(self, n, k):
# string = ''
# for i in range(n): string += str(i+1)
# Solution.count = 0
# self.dfs(n, k, string, '')
def getPermutation(self, n, k):
res = ''
k -= 1
fac = 1
for i in range(1, n): fac *= i
num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in reversed(range(n)):
curr = num[k/fac]
res += str(curr)
num.remove(curr)
if i !=0:
k %= fac
fac /= i
return res
[leetcode]Permutation Sequence @ Python的更多相关文章
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- LeetCode——Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Leetcode] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode]题解(python):060-Permutation Sequence
题目来源 https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! ...
- [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 ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- linux 卸载自带apache httpd 安装apache httpd
一.卸载自带apache httpd 1.关闭httpd服务:/etc/init.d/httpd stop 2.列出相关程序包:rpm -qa|grep httpd 3.卸载命令:rpm -e --n ...
- 对象本田CRV
<script> window.onload = function (ev) { var vcar = new Car("本田", "CRV", 4 ...
- ubuntu下java8卸载
要删除 OpenJDK (如果已安装的话).首先,检查是安装的哪个 OpenJDK包. # dpkg --list | grep -i jdk 移除 openjdk包: # apt-get purge ...
- Journal of BitcoinJ 从clone开始
启动Powershell cd D:\workspace mkdir BitcoinJ git init
- java 泛型 ? 和 T的区别
看了一个CSDN的问题,感觉就清楚了:http://bbs.csdn.net/topics/300181589/ 摘录其中的重点: 泛型方法: public <T extends Object& ...
- java集合进行排序的两种方式
java集合的工具类Collections中提供了两种排序的方法,分别是: Collections.sort(List list) Collections.sort(List list,Compara ...
- xhprof扩展安装与使用
目录 一.xhprof扩展安装步骤 二.xhprof的使用 总结 参考资料 一.xhprof扩展安装步骤 xhprof是PHP的一个扩展,最好也直接安装上graphviz图形绘制工具(用于xhprof ...
- MATLAB·提取图像中多个目标
基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...
- POJ 1182 食物链 【带权并查集】
<题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...
- 洛谷 P1474 货币系统 Money Systems(经典)【完全背包】+【恰好装满的最大方案数量】
题目链接:https://www.luogu.org/problemnew/show/P1474 题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对 ...