【一天一道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 ...
随机推荐
- linux下的静态库与动态库详解
静态库 先说说我们为什么需要库? 当有些代码我们大量会在程序中使用比如(scanf,printf等)这些函数我们需要在程序中频繁使用,于是我们就把这些代码编译为库文件,在需要使用时我们直接链接即可. ...
- ACM pie
我的生日快到了,传统上我正在做馅饼.不只是一个馅饼,不,我有N个,各种口味和各种尺寸. 数量为F我的朋友会来到我的聚会,每个人都得到一个馅饼. 这应该是一块馅饼,而不是几个小块,因为看起来很乱.这一块 ...
- Docker的Fig 项目
在你的应用里面添加一个 fig.yml 文件,并指定一些简单的内容,执行 fig up 它就能帮你快速建立起一个容器 快速搭建基于 Docker 的隔离开发环境 使用 Dockerfile 文件指定你 ...
- Linux: Check version info
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/version Linux version 2.6.1 ...
- Android的四大组件及应用安装安全问题(4)
Android的四大组件及组件间通信 如果想对四大组件有深入的了解,那永远不要停留在一些条条干干的SDK API的认识,需要了解他的通讯,他的复用,他的边界问题,这样才会对四大组件有透明的认识. 四大 ...
- iOS开发基础之开发证书的说明和发布
1.首先通过钥匙串访问--证书助理--从证书颁发机构请求证书--填写证书信息(邮箱,常用名称,存储到磁盘)--存储为(自定义名称.certSigningReuqest,简称CSR文件,只是为了提交到苹 ...
- ROS机器人程序设计(原书第2版)补充资料 (零) 源代码、资料和印刷错误修订等 2017年01月01日更新
ROS机器人程序设计(原书第2版)补充资料 (零) 源代码等 ROS官网 版)部分内容修订 页:第1行,删去$ 页:第6行,float64 y 前面加一个空格 页:中间创建主题:下面程序不用换行,(& ...
- JVM内存区域划分(JDK6/7/8中的变化)
前言 Java程序的运行是通过Java虚拟机来实现的.通过类加载器将class字节码文件加载进JVM,然后根据预定的规则执行.Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同 ...
- 1.cocos2dx 3.2环境搭建
1 所需软件 jdk-7u25-windows-i586.exe python-2.7.8.amd64.msi cocos2d-x-3.2.zip apache-ant-1.9.4.zi ...
- Ubuntu 安装 texlive2013 及中文支持
分享一下安装和配置经验. 1.材料准备 texlive的安装包:可以百度下,这里也提供一个下载地址: http://mirror.hust.edu.cn/CTAN/systems/texlive/Im ...