题目大意:给出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. 文档碎片及xml讲解

    1.将数据渲染到页面的几种方式 1.字符串拼接 2.dom循环 3.模板 4.文档碎片 字符串拼接: 优势:只进行一次dom回流 缺点:原有的dom事件会消失 案例分析:原有list中有3个li,并且 ...

  2. Jmeter(九)JDBC连接池

    JDBC为java访问数据库提供通用的API,可以为多种关系数据库提供统一访问.因为SQL是关系式数据库管理系统的标准语言,只要我们遵循SQL规范,那么我们写的代码既可以访问MySQL又可以访问SQL ...

  3. Find the peace with yourself

    The purpose of being mature is to find the real calm and peace with yourself. Or you can say the tur ...

  4. C#列的一些操作

    //变更列名 DataTableA.Columns["原来的列名"].ColumnName="新的列名";

  5. javaScrip字符串(String)相关

    -----------------------------------------此页只记录前端关于String的东西----------------------------------------- ...

  6. 图像生成-VAE简介

    VAE(Variational Autoencoder)   生成式模型 理论: 基于贝叶斯公式.KL散度的推导 1. 自动编码器的一般结构 2. 产生一幅新图像 输入的数据经过神经网络降维到一个编码 ...

  7. sublime 打开import require 模块文件的url 或路径的插件

    结果一番周折,终于发现sublime提供的一个插件(open url)可以实现打开import file 路径的文件,比如import demo from “../path” 的文件在新窗口或者新标签 ...

  8. Django-models的字段类型

    model的field类型 1.models.AutoField   ---自增列 = int(11)    如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设 ...

  9. VMware NAT模式多个虚拟机相互访问

    在一台主机上只允许有一个NAT模式的虚拟网络.因此,同一台主机上的多个采用NAT模式网络连接的虚拟机也是可以相互访问的.

  10. redis如何随系统启动

    Redis可以通过命令redis-server启动,但这种启动方式适用于开发环境,对于生产环境来说,配置好redis的配置文件,并使redis随linux启动则更加方便些,下面则记录下redis如何随 ...