[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):求指定位置的全 ...
随机推荐
- BZOJ2674 : Attack
整体二分+树状数组套Treap,时间复杂度$O(n\log^3n)$. #include<cstdio> #include<cstdlib> #include<algor ...
- BZOJ3825 : [Usaco2014 Nov]Marathon
不跳过任何点的路程=dis(l,l+1)+dis(l+1,l+2)+...+dis(r-2,r-1)+dis(r-1,r) 要跳过一个点i,则要最小化dis(i,i+2)-dis(i,i+1)-dis ...
- 在Windows8 Winrt中 高性能处理多个条件语句 用于实现自定义手势
http://blog.csdn.net/wangrenzhu2011/article/details/8578806 (转) 在winrt中 多点触控 控件的应用越来越多,例如 各种手势与 控件之间 ...
- Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
一.Servlet 生命周期 1.加载 2.实例化 3.初始化 4.处理请求 5.销毁 二.Servlet与cgi的区别: Servlet处于服务器进程中,它通过多线程方式运行其service方法,一 ...
- C++ sort vector<vector<int> > or vector<MyClass> 容器的排序
C++的STL中提供了很强大的排序函数sort,可以对任意数组,结构体及类进行排序,下面我们先来看最简单的数组排序.默认的升序排列,我们也可以在后面加上less或greater来告诉编译器我们想要的排 ...
- [转载] - QWidget、QMainWindow、QDialog和QFrame的区别
继承关系:在Qt中所有的类都有一个共同的基类QObject ,QWidget直接继承与QPaintDevice类,QDialog.QMainWindow.QFrame直接继承QWidget 类. ...
- Struts2 实战(一)
环境: Ubuntu 14.04 LTS 64位 开发工具的准备 我选择 Eclipse, 而没有选择MyEclipse, 一是因为免费,不想去弄破解,二是不想太傻瓜化的东西(注:本人并没有用过MyE ...
- Mui - 全局css
头部(mh) <header class="mui-bar mui-bar-nav"> <a class="mui-action-back mui-ic ...
- 红,X-Japan
ktv如果唱这首应该很爽,慢慢学. 红(くれない) 歌手:X JAPAN作词:YOSHIKI作曲:YOSHIKI I could not look back you'd gone away from ...
- p2p网贷项目开发全过程技术详解,应用框架是ci2.2
p2p网贷项目开发全过程技术详解,应用框架是ci2.2 很标准的mvc开发代码,代码也很简单,方便二次开发 这篇文章会不断更新