[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):求指定位置的全 ...
随机推荐
- shell运算
- python 操作execl文件
http://www.jb51.net/article/60510.htm import xlrdimport xlwt # 打开文件 workbook = xlrd.open_workbook( ...
- ubuntu apt源
deb http://archive.ubuntu.com/ubuntu/ vivid main restricted universe multiversedeb http://archive.ub ...
- 李洪强-C语言3-数组
一.数组的概念 用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. 二.数组的定义 格式: 类型 数组名[元素个数]: 举例:存 ...
- mysql 动态sql语句
; SET @ss = ' INSERT INTO prod_attr SELECT ? AS prod_id,A.* FROM ( SELECT 1 AS attr_id,\'中国出版社\' AS ...
- 对Get-Content参数-readcount的解释
绝大多数用户更关心最新的日志,下面给出一个简单的例子演示从一个文本日志中获取最后的某几行文本行: # 显示windowsupdate.log 文件的最新5行日志 $logs = Get-Conte ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- OpenCV学习笔记——形态学梯度操作
代码: #include<cv.h> #include<highgui.h> int main(void) { cvNamedWindow("cmp"); ...
- HDU 2795 Billboard(线段树的另类应用)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 推荐的PHP编码规范
推荐的PHP编码规范 发布时间: 2014-05-7 浏览次数:2754 分类: PHP教程 推荐的PHP编码规范 一 编辑器设置 1. 使用Tab缩进,不要使用空格 鉴于很多编辑器在保存文件时会自动 ...