给出集合 [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. yii2使用vendor文件夹下的的css文件

    yii2 使用 vendor 下在css样式, 可以新建一个Asset,定义 public $sourcePath="@vendor/..",如:public $sourcePat ...

  2. CF822F Madness

    题意:给你一棵边权都为1的树,要求选择互不相交的若干条路径,这些路径包含有所有点. 在每一条路径上选择一条边,放置一个动点,设置一个方向,它开始在该路径上来回运动,速度为1.每个点上都有一个停表,当有 ...

  3. Jupyter notebook文件默认存储路径以及更改方法

    1.文件默认存储路径怎么查?  安装Anaconda后,新建文件的默认存储路径一般在C系统盘,那么路径是什么呢? 首先,新建一个.ipynb文件, 输入以下脚本,运行出的结果即是当前jupyter文件 ...

  4. 20170702-变量说明,静态方法,类方法区别,断点调试,fork,yield协程,进程,动态添加属性等。。

    概念: 并行:同时运行 并发:看似同时运行  json后任然中文的问题 import json d = {"名字":"初恋这件小事"} new_d1 = jso ...

  5. Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)

    转:http://www.cnblogs.com/LingzhiSun/archive/2011/06/13/EFPowerTool_2.html   上次为大家介绍EF Power Tool之后,不 ...

  6. JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案

    现在很多系统都有导出excel的功能,总结一下自己之前写的,希望能帮到其他人,这里我用的是XSSFWorkbook,我们项目在winsang 用的Tomcat,LInux上用的weblogic服务器, ...

  7. idea从github中pull或者push成功之后ssm项目全部controller报红色下划线的解决方案

    某次从github上pull下来之后,报出如下一堆红色波浪线错误 解决方法: 在随便一个红色波浪线处,按下alt+enter,点击add maven dependency, 选中所有,点击添加即可,此 ...

  8. 解析Asp.net Core中使用Session的方法

    2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. 元旦放假在家写了个Asp.net Core验证码登录, 做demo的过程中遇到两个小问题,第一是在Asp.net Core中引 ...

  9. 收藏的链接-Qt

    Qt编写的开源帖子集合(懒人专用) - QTCN开发网 - Powered by phpwind http://www.qtcn.org/bbs/read-htm-tid-85501.html?tds ...

  10. Java开发系列-JDBC

    概述 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和 ...