题目大意:给出n和k,找到1..n这些数组成的有序全排列中的第k个。

首先,n的全排列可以分成n组,每一组由n-1个数组成。

例如  3的全排列,分成三组:

1  2  3  和 1  3  2

2  1  3  和 2  3  1

3  1  2  和 3  2  1

每一组的个数是(n-1)!,每一组的打头的都是i 。第i组以i打头。

先求 x = (k-1) / (n-1)!  + 1

比如  n = 3, k = 4.

x = 3/2 + 1 = 2,得到 第四个实际上第二组里面。

y = (k-1)%(n-1)! + 1 = 2,即,要求的东西在第二组的第二个数。

第二组的特征是,以2开头。所以我们找到第2个数,放到string ans的最高位置。

然后,求剩下的数里面的全排列的第y个。 即把2去掉,用1 和 3做全排列,取其中的第2个。

这时候,回到初始的步骤。递归即可。

垃圾的代码,现在脑子懵。。写的代码跟屎一样,完全没有逻辑啊!要优化!!

class Solution {
public:
int factorial(int n) //计算n的阶乘
{
if(n == || n == ) return ;
return n*factorial(n-);
} void back_track(vector<int>&used, string &ans, int n, int cnt, int k) //cnt表示ans里有几个元素。找到第k个。
{
if(cnt == n) return; //所有的都用完了 int fac = factorial(n--cnt); //初始是n-1 int x = (k-)/fac + ; //在第x 个序列 第一个数是第几个
k = (k-)%fac + ; //x序列的第k个 int i,j; i = ; while(used[i]) i ++; //找到第一个没用过的元素
for(j = ; j < x;i++) //这时候x指向的是第1个未使用的元素
{
if(!used[i]) j++; //此时x走过了j-1个元素。
}
while(used[i]) i++; //找到第x个未使用的元素 ans.append(,i + ''); //把这个元素放在头
used[i] = true; //这个元素被使用了
back_track(used, ans, n, cnt + ,k); //用剩下的去构造第k个元素
}
string getPermutation(int n, int k) {
int i;
vector<int> used(n+,false);
string ans;
back_track(used,ans,n,,k);
return ans;
} };

LeetCode 题解 Permutation Sequence 需要优化!的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. 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 ...

  3. [LeetCode 题解]: Permutation Sequcence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. [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 ...

  5. 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 ...

  6. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  8. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  9. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. Jmeter(十二)关联

    关联在实际业务需求中是随处可见的,比如:支付需要提交订单成功的订单号:修改个人资料需要登录成功响应报文信息...总之关联无处不在,今天来记一记Jmeter的关联功能. Jmeter关联的方法比较常用的 ...

  2. [UE4]Delay与Retriggerable Delay

    一.Delay 在右上角都有一个表盘的图标,表示不是在当帧内执行,而是需要一定时间才能完成的. 鼠标移上去,会有一段文字注释.根据指定的延迟时间执行一个延后的动作.当计时还没有结束的时候,如果再次被调 ...

  3. 关于daterangepicker的配置

    一开始接触daterangepicker搞得思路很乱,慢慢研究才了解一些,下面粘一个daterangepicker的基本配置,代码是来自网上某位大神的.我只是引荐过来的,加入了周和月的汉化. 在回调函 ...

  4. 第二章:安装zabbix过程

    2.2 安装zabbix过程 2.2.1 安装方式选择 编译安装 (服务较多,环境复杂) yum安装(干净环境) 使用yum 需要镜像yum源 http://www.cnblogs.com/clsn/ ...

  5. 工作T-SQL备忘

    作为一个"浸淫" Oracle 数据库很久的人来说, 突然转入 T-SQL, 也就是 MSSQL , 工作中经常用的查询和 MSMS 使用备忘如下 : --1. 切换对应的库连接 ...

  6. POJ K-th Number

    [题解] 数据结构采用线段树.通过将数组的每一段归并排序来建树.将数组排序来实现离散化. 时间复杂度分析:建树的过程就是归并排序,其时间复杂度为O(nlog(n)).查询时:二分查找第k小元素的复杂度 ...

  7. Linux图形操作与命令行

    一.执行命令 通过shell 在哪里输入: 1. 字符界面 2. 终端模拟器程序,如gnome-terminal.konsole (最早的linux是没有图形界面的,只有tty,也就是字符终端.当有了 ...

  8. spark 2.0.0集群安装与hive on spark配置

    1. 环境准备: JDK1.8 hive 2.3.4 hadoop 2.7.3 hbase 1.3.3 scala 2.11.12 mysql5.7 2. 下载spark2.0.0 cd /home/ ...

  9. (转)C# WebApi 异常处理解决方案

    原文地址:http://www.cnblogs.com/landeanfen/p/5363846.html 一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发 ...

  10. 互斥锁,IPC队列

    进程同步(锁) 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,part1:共享同一打印终端,发现会有多行内容打印到一行的现象(多个进程共享并抢占同一个 ...