【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列
(一)题目
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):
1:”123”
2:”132”
3 : “213”
4 :”231”
5 : “312”
6 : “321”
Given n and k, return the kth permutation sequence.
(二)解题
第一种解法:
参考:【一天一道LeetCode】#31. Next Permutation这篇博客,很奇怪的是为什么n=8,k=8590的时候会超时,暂时还没有想出来原因,欢迎大家留言讨论。
/*
利用STL的next_permutation每次求出下一个排列数
*/
class Solution {
public:
string getPermutation(int n, int k) {
string seq;
string ret;
for(int i = 0 ; i < n ; i++)
{
seq+=('1' + i);
}
do
{
k--;
if(k==0) {
ret = seq;
break;
}
}while(next_permutation(seq.begin(),seq.end()));
return ret;
}
};
第二种解法:
利用排列数的规律来求解。
我们以题目给出的例子为例来讲解规律。 n=3时,由1,2,3三个数组成{a1,a2,a3}
1:”123”
2:”132”
3 : “213”
4 :”231”
5 : “312”
6 : “321”
首先看第一个数a1(候选数字temp = “123”),k为1,2时,a1=1,k为3,4时,a1=2,k为5,6时,a1=3,从中可以看出a1=temp[(k-1)/(n-1)!]。
确定了第一个数后我们来看第二个数a2,以1,2这一组来看,排除了1,候选数字temp = “23”,这个时候k只能为1和2,所以在上一步中k%=(n-1)!,这个时候a2=temp[(k-1)/(n-2)!]
最后a3只能为剩下的一个数字了。
ok,整理一下思路,我们需要一个候选字符串temp,一个(n-1)!的数组f[10] = {1,1,2,6,24,120,720,5040,5040*8}(n为1~9的数字),k初始值为k-1
在每一步中,a1=temp[k/f[n-1]],k%=f[n-1]。一直到n为0。
具体思路见代码:
class Solution {
public:
string getPermutation(int n, int k) {
string temp = "123456789";
int f[] = {1,1,2,6,24,120,720,5040,5040*8};
string ret;
int i = n;
k--;//k的初始值
while(i)
{
int m = k/f[i-1];
k %=f[i-1];
ret+=temp[m];
temp.erase(temp.begin()+m);//擦除掉已经选掉的值
i--;
}
return ret;
}
};
【一天一道LeetCode】#60. Permutation Sequence.的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [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 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 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 the p ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Docker的名字空间
名字空间是 Linux 内核一个强大的特性.每个容器都有自己单独的名字空间,运行在其中的应用都像是在独立的操作系统中运行一样.名字空间保证了容器之间彼此互不影响. pid 名字空间 不同用户的进程就是 ...
- Win7 环境下虚拟机内 Samba 服务器的安装、配置以及与主机的通信实现
考虑到window和linux虚拟机之间互传文件较为麻烦,遂打算在虚拟机中安装Samba服务器,以此实现共享文件给window使用.然而安装配置过程曲折,遂作记录如下: 一.samba服务器的安装 正 ...
- Arrays的二分查找
二分查找也称为折半查找,是对有序元素查找的一种算法,在查找的过程中,不断的将搜索长度减半,因此效率不错.Java的JDK提供了二分法查找的算法,使用的方法是Arrays.binarySearch(). ...
- Spring中@Transactional事务回滚(含实例详细讲解,附源码)
一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用.下面举个栗子:比如一个部门里面有很多成员,这两者分别保存在部门表和成员表里面,在删除 ...
- Android中Sqlite数据库进行增删改查
今天这篇文章写Sqlite数据库,通过一个小案例来完整讲一下数据库常见的CRUD操作. 先对知识点总结: SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHel ...
- Spring+EhCache缓存实例(详细讲解+源码下载)
一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...
- MPAndroidChart——饼图
MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...
- Hadoop与分布式数据处理 Spark VS Hadoop有哪些异同点?
Spark是一个开源的通用并行分布式计算框架,由加州大学伯克利分校的AMP实验室开发,支持内存计算.多迭代批量处理.即席查询.流处理和图计算等多种范式.Spark内存计算框架适合各种迭代算法和交互式数 ...
- introduction of velocity
一.velocity 简介 基于java 的模板引擎,apache 旗下的开源软件项目. 目的在于隔离 表示层和业务逻辑层,当然现在做的不仅仅是这些. 二.应用场景 web 应用程序:创建html页面 ...
- spring 的OpenSessionInViewFilter简介
假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...