/*
n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数
*/
public String getPermutation(int n, int k) {
k--;
List<Integer> list = new ArrayList<>();
StringBuilder res = new StringBuilder();
int count =1;
//以每个数字开头的集合有多少中排列
for (int i = 2; i <= n -1; i++) {
count*=i;
}
//记录哪些数字还没用
for (int i = 1; i <=n ; i++) {
list.add(i);
}
//回合数,也就是小集合的size
int round = n-1;
while (round>=0)
{
int num = list.get(k/count);
res.append(num);
list.remove(k/count);
if (round>0)
{
k = k%count;
count/=round;
}
round--;
}
return res.toString();
}

[LeetCode]60. Permutation Sequence求全排列第k个的更多相关文章

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

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

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

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

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

  6. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  7. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

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

  9. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. C语言printf()函数的格式化字符串

    原文链接:https://www.runoob.com/cprogramming/c-function-printf.html#include<stdio.h> #include<s ...

  2. 极简Linux下安装极简桌面

    sudo apt install -y xorg lxde-core vnc4server 设置密码:vncpasswd 然后先开启服务,然后再终止服务:(这是为了创建一个默认的配置文件)vncser ...

  3. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中的topLevelItem、indexOfTopLevelItem和takeTopLevelItem方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 获取指定位置顶层项 树型部件QTreeWidget中,通过topLevelItem方法根据位置索引取 ...

  4. PyQt(Python+Qt)学习随笔:QAbstractItemView的defaultDropAction属性

    老猿Python博文目录 老猿Python博客地址# 一.概述 defaultDropAction属性用于控制QAbstractItemView及其子类的实例视图中拖放时放下的默认操作.该属性的类型为 ...

  5. PHP代码审计分段讲解(3)

    05 ereg正则%00截断 放上源代码 <?php $flag = "flag"; if (isset ($_GET['password'])) { if (ereg (& ...

  6. 如何在苹果电脑上创建一个html格式文件,并在浏览器正确打开

    之前一直使用windows系统的电脑,创建文件很简单,改格式也非常的简单.但换了苹果电脑,如何创建一个HTML文件?却把我给整蒙了. 首先,为什么mac上不能直接新建文本文件? 因为mac一都是以应用 ...

  7. springboot 导出xlsx文件

    @Override public String downModelXlsFile(SxSourceReq param, HttpServletResponse response) { //设置响应编码 ...

  8. SA-IS学习笔记

    Q:SA-IS 是什么玩意? A:一种 \(O(n)\) 求后缀数组的高科技. Q:为什么会有 SA-IS 这种算法? A:因为它是 \(O(n)\) 的,比倍增 \(O(n\log n)\) 快. ...

  9. AtCoder Grand Contest 017 (VP)

    contest link Official Editorial 比赛体验--之前做题的时候感觉 AtCoder 挺快的,现在打了VP之后发现还是会挂的--而且不是加载缓慢或者载不出来,直接给你一个无法 ...

  10. 学生证申请Idea

    1.地址:https://www.jetbrains.com/shop/eform/students2.英语不好的一键翻译,说明:三五天下来3.上图: