[LeetCode]题解(python):060-Permutation Sequence
题目来源
https://leetcode.com/problems/permutation-sequence/
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.
题意分析
Input: n, k
Output: the kth permutation in permutations based on n
Conditions: 返回第n个排列
题目思路
看了网上答案发现还有这样一个方法,每一位都是nums[k/factorial],注意k要从0计数所以要减一,nums要及时移除已使用过的元素(不移除可以用False or True数组标记),factorial一般是n-1的阶乘,以上三个数都要更新
AC代码(Python)
class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
res = "" nums = [i + 1 for i in xrange(n)]
#count from 0
k -= 1
fac = 1
for i in xrange(1, n):
fac *= i
for i in reversed(xrange(n)):
index = k / fac
value = nums[index]
res += str(value)
nums.remove(value)
if i != 0:
k %= fac
fac /= i
return res
[LeetCode]题解(python):060-Permutation Sequence的更多相关文章
- 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 ...
- 【LeetCode】060. 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 th ...
- 060 Permutation Sequence 排列序列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,可得到如下序列 (例如, n = 3): 1."123" 2. & ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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] 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):求指定位置的全 ...
随机推荐
- BZOJ2725 : [Violet 6]故乡的梦
如果S==T,那么答案为0. 如果S与T不连通,那么答案为inf. 否则,S到T的最短路径上至少有一条边. 求出以S为源点的最短路图,是个DAG,随便抓一条S到T的最短路,记为P. 设dpS[x]表示 ...
- 【POJ】1743 Musical Theme
http://poj.org/problem?id=1743 题意:不可重叠最长重复子串,n<=20000,具体看<后缀数组>-- 罗穗骞 #include <cstdio&g ...
- Chromium源码--视频播放流程分析(拨开云雾)
转载请注明出处: http://www.cnblogs.com/fangkm/p/3791964.html 在PC浏览器中播放视频,大部分视频网站都是采用flash播放器,这多亏了Adobe Flas ...
- linux rootfs制作
http://blog.sina.com.cn/s/blog_6795385f01011ifg.html 作一个嵌入式Linux rootfs,并且实现 web 服务 1. 文件系统简介 •理论上说一 ...
- oracle中用SQL实现两个日期间的日期形成一个数据集
比如输入2014-06-1 和 2014-07-1形成一个2014-06-1 2014-06-22014-06-3...2014-07-1 的数据集. 解决方法: select date'2014 ...
- JBPM4.4与SSH2之整合
JBPM4.4与SSH2之整合(附完整源码) 这是我来到这世上二十多年来,第二次写博客啊.哈哈 这些天,想着把jbpm4.4与ssh2整合一下,由于之前从来没用过jbpm,也不知道各版本之间会有什么 ...
- mysql 动态sql语句
; SET @ss = ' INSERT INTO prod_attr SELECT ? AS prod_id,A.* FROM ( SELECT 1 AS attr_id,\'中国出版社\' AS ...
- C# - Lee 公共类库
我的公共类库 using System; using System.IO; using System.Net; using System.Security.Cryptography; using Sy ...
- Ajax解决IE浏览器兼容问题
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享. 因为Context ...
- Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...