给出集合 [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_20191118【常用模块的用法 Json、pickle、collections、openpyxl】

    序列化模块 序列化:将python或其他语言中的数据类型,转变成字符串类型. python中的八大数据类型回顾:int float str list tuple dict set bool 反序列化: ...

  2. 阿里云发布新版SaaS上云工具包,全面助力SaaS上云

    9月26日,在云栖大会SaaS加速器专场上,阿里云发布了新版的SaaS上云工具包(SaaS Launch Kit),发布了API网关的新功能,以及推出了全新升级的能力中心. SaaS上云工具包,顾名思 ...

  3. Vue+Iview+Node 安装环境 运行测试Vue

    1.运行环境及设置 备注:建议设置 npm config set registry https://registry.npm.taobao.org 2.全局安装vue/cli 3.创建vue 项目 v ...

  4. 初步了解Redis

    参考: https://juejin.im/post/5b4dd82ee51d451925629622?utm_source=gold_browser_extension https://www.cn ...

  5. IDEA 安装lombok及使用

    1.File-Settings-Plugins-Brows Repositories-输入lombok-install 2.重启idea 3.添加maven依赖 <dependency> ...

  6. Vue基础(1)

    目录 Vue基础 基础 导入 1. 挂载 2. 插值表达式 3. 事件 4. 创建对象 5. v-text和v-html 6. vue的过滤器 7. 属性指令 Vue基础 基础 首先我们要知道Vue是 ...

  7. selenium基础(参数化脚本)

    参数化脚本 什么是参数化 参数化就是用包含多组数据的参数列表,使之替换脚本中的响应常量值,这样,在脚本运行的时候,就会使用参数表中的数据来代替脚本中的常量值 由于参数表中包含了多组数据,所以执行用例时 ...

  8. 2019个人计划与Flag与期望

    突然发现写博客是真的好. 希望未来能在其他地方写上日记. 总结2018中的个人缺陷: 1.忘掉了学习方法或者说学习方法不正确 2.偶尔就会去偷下懒,对自己不够严格,自控能力差. 3.心态虽比以前好很多 ...

  9. 乌云精华漏洞整合(epub)

    还是7月份的 链接: http://pan.baidu.com/s/1kUGIOez 密码: gfqp

  10. [记]Windows 系统下设置Nodejs NPM全局路径

    Windows下的Nodejs npm路径是appdata,担心安装的node_modules越来越多,导致C盘满,所以参考别人的博文,将node_modules安装的默认目录修改一下. 参考Wind ...