[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 ...
随机推荐
- eclipse 更换主题
更换系统自带主题 依次点击 window->preferences->General->Appearance->Theme 选择主题 下载eclipse marketplace ...
- Java NIO系列教程(一)java NIO简介
这个系列的文章,我们开始玩一玩IO方面的知识,对于IO和NIO,我们经常会接触到,了解他们的基本内容,对于我们的工作会有特别大的帮助.这篇博文我们仅仅是介绍IO和NIO的基本概念,以及一些关键词. 基 ...
- [九省联考2018]一双木棋chess
题解: 水题吧 首先很显然的是状压或者搜索 考虑一下能不能状压吧 这个东西一定是长成三角形的样子的 所以是可以状压的 相邻两位之间有几个0代表他们差几 这样最多会有2n 然后就可以转移了 由于之前对博 ...
- C#资源管理器
窗体搭建:ContextMenuStrip右键菜单,Treeview树形菜单,Listview控件 新建"我的文件"类: public class MyFile { public ...
- php 三元运算符实例详细介绍
三元运算符的功能与“if....else”流程语句一致,它在一行中书写,代码精练.执行效率高.在PHP程序中恰当地使用三元运算符能够让脚本更为简洁.高效.代码的语法如下: ? 1 (expr1)?(e ...
- HDU 5391 Zball in Tina Town【威尔逊定理】
<题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...
- 计蒜客 无脑博士的试管们 【dfs】
题目链接:https://nanti.jisuanke.com/t/31 题目大意: 无脑博士有三个容量分别是A,B,C 升的试管,A,B,C 分别是三个从 1 到20 的整数,最初,A 和 B 试管 ...
- Java设计模式从精通到入门三 策略模式
介绍 我尽量用最少的语言解释总结: Java23种设计模式之一,属于行为型模式.一个类的行为或者算法可以在运行时更改,策略对象改变context对象执行算法. 应用实例: 以周瑜赔了夫人又折兵的例 ...
- Playmaker全面实践教程之简单的使用Playmaker示例
Playmaker全面实践教程之简单的使用Playmaker示例 简单的使用Playmaker示例 通过本章前面部分的学习,相信读者已经对Playmaker有了一个整体的认识和印象了.在本章的最后,我 ...
- ajax那些事儿
一.Ajax的定义浏览器与服务器之间,采用HTTP协议通信.用户在浏览器地址栏键入一个网址,或者通过网页表单向服务器提交内容,这时浏览器就会向服务器发出HTTP请求.Ajax全称Asynchronou ...