给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。

按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

给定 n 和 k,返回第 k 个排列。

说明:

  • 给定 n 的范围是 [1, 9]。
  • 给定 k 的范围是[1,  n!]。

示例 1:

输入: n = 3, k = 3 输出: "213"

示例 2:

输入: n = 4, k = 9 输出: "2314"

较为低效的回溯做法

class Solution {
public:
int N;
int K;
int cntk;
vector<bool> visit;
string res;
string getPermutation(int n, int k)
{
N = n;
K = k;
cntk = 0;
visit = vector<bool>(n + 1, false);
DFS(n, 0);
return res;
} bool DFS(int cnt, int num)
{
if(cnt == 0)
{
cntk++;
if(cntk == K)
{
res = to_string(num);
return true;
}
else
return false;
}
for(int i = 1; i <= N; i++)
{
if(visit[i] == true)
continue;
visit[i] = true;
num = num * 10 + i;
if(DFS(cnt - 1, num))
{
return true;
}
visit[i] = false;
num = (num - i) / 10;
}
return false;
}
};

Leetcode60. Permutation Sequence第k个排列的更多相关文章

  1. LeetCode60:Permutation Sequence

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

  2. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. Permutation Sequence(超时,排列问题)

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

  4. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

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

  5. 60. Permutation Sequence(求全排列的第k个排列)

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

  6. LeetCode 60. 第k个排列(Permutation Sequence)

    题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "1 ...

  7. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  8. 【LeetCode每天一题】Permutation Sequence(排列序列)

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

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

随机推荐

  1. CSIC_716_20191029【人脸打分系统】

    今日内容: 1.调用百度的AI接口,完成人脸图像打分( 敷衍) 2.完成系统内置时间的打印 3.将上述两段代码生成可执行文件 ------------------------------------- ...

  2. 计算几何——直线交点poj1269

    求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...

  3. error LNK2001: unresolved external symbol _main解决办法(zz)

    error LNK2001: unresolved external symbol _main解决办法   解决外部符号错误:_main,_WinMain@16,__beginthreadex -!t ...

  4. System.Web.Mvc.IAuthorizationFilter.cs

    ylbtech-System.Web.Mvc.IAuthorizationFilter.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutra ...

  5. 国外主机如何ICP备案

    想都不要想了,无法备案. 因为,备案是在主机服务器提供商处的备案平台提交申请,国外的主机服务商是没有这种平台服务的.(跟你域名在哪儿买的没关系) 下面,把昨天折腾到半夜的过程记录一下,希望可以帮到需要 ...

  6. 04.基本数据类型(list,tuple)

    本节主要内容:1. 列表2. 列表的增删改查3. 列表的嵌套4. 元组和元组嵌套5. range一. 列表1.1 列表的介绍 列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. ...

  7. KVM热添加技术

    1.KVM热添加的种类 硬盘.网卡.内存.cpu 2.KVM热添加硬盘 1.在宿主机上创建硬盘 qemu-img create -f qcow2 /opt/web02_add01.qcow2 10G ...

  8. East Central North America 2006 Hie with the Pie /// 状压dp oj22470

    题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...

  9. 帝国cms学习

    手册地址1 手册地址2 入门 安装: 将下载的upload里的文件上传到网站更目录 然后 域名/e/install/index.php Warning: Use of undefined consta ...

  10. 2019-8-31-C#-如何给-ValueTuple-返回值添加注释

    title author date CreateTime categories C# 如何给 ValueTuple 返回值添加注释 lindexi 2019-08-31 16:55:58 +0800 ...