60. Permutation Sequence(求全排列的第k个排列)
[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.
如果用前面2道题 的递归 非递归 都会超时。
只能用数学的方法计算。
class Solution(object):
def getPermutation(self, n, k):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
k = k -1
nums = list(range(1, n + 1))
res = ''
while len(nums)!=0:
ai =int(k/self.fn(n-1))
res += str(nums[ai])
del nums[ai]
k = k % self.fn(n-1)
n = n - 1
return res
def fn(self, n):
res = 1
while n != 0:
res = res * n
n = n - 1
return res
60. Permutation Sequence(求全排列的第k个排列)的更多相关文章
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 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 ...
- 60. 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 t ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode OJ 60. Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...
- 60. Permutation Sequence (String; Math)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- logback参考配置
logback配置 <?xml version="1.0" encoding="UTF-8"?> <configuration> < ...
- js 滚动页面
$(‘html, body’).animate({ scrollTop: 0}, ‘slow’);
- windows下解决mysql忘记password
windows下解决mysql忘记password mysql有时候忘记password了怎么办?我给出案例和说明!一下就攻克了! Windows下的实际操作例如以下 1.关闭正在执行 ...
- python3----练习题(过滑块验证)
# 导入模块 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webd ...
- CentOS中Apache虚拟主机(virtualHost)设置在/home目录下的若干问题
在Ubuntu中安装LAMP是非常简单的意见事情.但是在CentOS中却遇到了很多问题. 首先是CentOS中必须手动配置iptables,把80端口开放出来,不然,是访问不到的,开放80端口在/et ...
- JavaScript------一元运算符+的使用
var y = "5"; // y 是一个字符串 var x = + y; // x 是一个数字 var y = "John"; // y 是一个字符串 var ...
- 浅述python中range()函数的用法
函数用法说明: 用法一:range(m) 输出: [0,1,...,m-1](从0到m-1的一个list,不包括m) 示例: 用法二:range(m,n),m<n 输出:[m,m+1,..,n- ...
- ng-template
一,锚点链接 定义锚点: <a href="#login" class="btn btn-primary">Login</a> < ...
- [POJ] Brackets Sequence
This problem can be solved elegantly using dynamic programming. We maintain two arrays: cnt[i][j] -- ...
- GRPC使用错误排查记录
1. 编译报错 f.fr.SetReuseFrames undefined (type *http2.Framer has no field or method SetReuseFrames) 该问题 ...