1. 原题链接

https://leetcode.com/problems/permutation-sequence/description/

2. 题目要求

给出整数 n和 k ,k代表从1到n的整数所有排列序列中的第k个序列,返回String类型的第k个序列

3. 解题思路

首先我们要知道这个序列是按照什么规律排列下去的,假如此时n=4,k= 21,n=4时所有的排列如下:

可以看出 n=4 时,一共有 4!=24种排列组合。

每一个数字开头各有 6 种排列组合,因此我们可以把同一数字开头的排列看作同一组,一共 4 组。

我们进一步探寻排列的规律。

(1) 第一步: 确定第一个数字

k=21,也就是要我们找到第19个排列组合,这个组合的第一个数字我们使用  (21-1)/(4-1)! = 3 ,3对应未使用数字中的第四位数字“4”,所以第一位数字为4。

将 4 从未使用数字中去除,还剩:1 2 3

解释一下为什么要 21-1:因为java进行整出运算时不会进行四舍五入,只保留整数不分。18/6 和 21/6 的结果都是3,按照每一个数字开头有 6 种排列方式,第 18和第 21 都是以 4开头。但实际上第 18 个排列以 “3” 开头,第 21 个以 “4” 开头。所以使用k-1来避免这个问题。

2)第二步:确定第二个数字

我们已经确定了第一位数字,也就是第一位数字是 4 ,第4组。从上面的排列组合可以看出,第二位存在三种数字,每一个数字都存在两次(蓝框圈出),因此第二位数字相同的又可以看成同一组。

k= 20%(4-1)! =20%6 =2, 2/(4-2)! = 2/2 =1, 1对应未使用数字中的第二位数字 “1”,因此第二位数字为2。

将2从未使用数字中去掉,还剩:1 3

(3)第三步:确定第三个数字

第三个数字只存在两种可能了,k= 2%(4-2)! =2%2 =0,0/(4-3)!= 0/1 =0,0对应未使用数字中的第一位数字 “1”,因此第三位数字为1

将1从未使用数字中去掉,还剩:3

(4)第四步:确定第四个数字

k=0%(4-4)! = 0%1 = 0,0/(4-4)!=0/1 =0 ,0对应第一位数字,此时未使用数字中的第一位数字“3”,因此第四位数字为3.

所以第 21 个排列组合为:4213

4. 代码实现

import java.util.ArrayList;
import java.util.List; public class PermutationSequence60 {
public static void main(String[] args) {
System.out.println(getPermutation(4,21));
System.out.println(19/6);
}
public static String getPermutation(int n,int k){
int pos = 0;
List<Integer> numbers = new ArrayList<>();
int[] factorial = new int[n+1];
StringBuilder sb = new StringBuilder(); int sum = 1;
factorial[0] = 1;
// 保存不同整数的阶乘
for(int i=1; i<=n; i++){
sum *= i;
factorial[i] = sum;
}
// factorial[] = {1, 1, 2, 6, 24, ... n!} // 未使用数字列表
for(int i=1; i<=n; i++){
numbers.add(i);
} k--; for(int i = 1; i <= n; i++){
System.out.println(factorial[n-i]);
int index = k/factorial[n-i];
sb.append(String.valueOf(numbers.get(index)));
numbers.remove(index);
k =k%factorial[n-i];
} return String.valueOf(sb);
}
}

  

LeetCode: 60. Permutation Sequence(Medium)的更多相关文章

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

  4. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  5. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

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

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

  8. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

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

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

随机推荐

  1. 【Hankson 的趣味题】

    可能我只适合这道题的50分 但还是要争取一下的 我们知道对于\(gcd\)和\(lcm\)有这样的定义 \(a=\prod _{i=1}^{\pi(a)}p_i^{d_{i}}\) \(b=\prod ...

  2. POJ 1845 Sumdiv 【二分 || 逆元】

    任意门:http://poj.org/problem?id=1845. Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions ...

  3. 备份&添加无线网络配置

    netsh wlan export profile key=clear folder=c:\ #备份 (ls c:\*.xml).FullName|%{netsh wlan add profile f ...

  4. phpstorm下TODO注释

    TODO注释 什么是TODO注释? 标记的注释,表示你代办的任务. 作用 标记你需要编写的任务位置 使用方法 TODO: + 说明: 如果代码中有该标识,说明在标识处有功能代码待编写,待实现的功能在说 ...

  5. 使用jQuery实现伪分页

    在之前的项目中遇到一个这样的问题,页面分为上下两部分(分别称为未选中设备信息部分和选中设备信息部分),上面是从数据库拿出来的所有的设备信息,下面是显式已选中的设备信息,页面如下所示: 可以选中其中的任 ...

  6. 【题解】洛谷P1941 [NOIP2014TG] 飞扬的小鸟(背包DP)

    次元传送门:洛谷P1941 思路 从题意可知 在每个单位时间内 可以无限地向上飞 但是只能向下掉一次 所以我们可以考虑运用背包解决这道题 上升时 用完全背包 下降时 用01背包 设f[x][y]为在坐 ...

  7. IOS9中使用NSURLConection发送异步网络请求

    IOS9中使用NSURLConection发送异步网络请求 在ios9中,NSURLConection的sendSync..和sendAsync已经过时.被NSURLSession代替. 以下蓝色部分 ...

  8. 前端基础-CSS的属性相关设置

    一 字体属性 二 文本属性 三 背景属性 四 盒子模型 五 盒子模型各部分详解 一. 字体属性 1.font-weight:文字粗细(表格中*为重点) 取值 描述   normal 默认值,标准粗细 ...

  9. Maven项目改为spring boot项目的方法

    目录树 新建Maven项目及步骤 修改方法 启动测试 新建Maven项目及步骤 我这里是从创建开始讲,使用的工具是Idea2017版本.如果是已经创建了Maven,想改为spring boot项目的请 ...

  10. js检测是否可以访问公网服务器

    wifi认证开发过程所用到的,源码如下: 注:检测AC是否放行成功,是否可以访问公网阿里云服务器 功能调用: checkNet().then(function(res) { if(res) { //连 ...