给出集合 [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之间(包括9)。
详见:https://leetcode.com/problems/permutation-sequence/description/

Java实现:

在数列 1,2,3,... , n构建的全排列中,返回第k个排列。
对于n个数可以有n!种排列;那么n-1个数就有(n-1)!种排列。
那么对于n位数来说,如果除去最高位不看,后面的n-1位就有 (n-1)!种排列。
所以,还是对于n位数来说,每一个不同的最高位数,后面可以拼接(n-1)!种排列。
所以可以看成是按照每组(n-1)!个这样分组。
利用k/(n-1)!可以取得最高位在数列中的index。这样第k个排列的最高位就能从数列中的index位取得,此时还要把这个数从数列中删除。
然后,新的k就可以有k%(n-1)!获得。循环n次即可。同时,为了可以跟数组坐标对其,令k先--。

class Solution {
public String getPermutation(int n, int k) {
k--;//to transfer it as begin from 0 rather than 1 List<Integer> numList = new ArrayList<Integer>();
for(int i = 1; i<= n; i++){
numList.add(i);
} int fac = 1;
for(int i = 2; i < n; i++) {
fac *= i;
} StringBuilder res = new StringBuilder();
int times = n-1;
while(times>=0){
int indexInList = k/fac;
res.append(numList.get(indexInList));
numList.remove(indexInList); k = k%fac;//new k for next turn
if(times!=0){
fac = fac/times;//new (n-1)!
}
times--;
} return res.toString();
}
}

参考:https://www.cnblogs.com/springfor/p/3896201.html

060 Permutation Sequence 排列序列的更多相关文章

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

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

  2. Java for LeetCode 060 Permutation Sequence

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

  3. 【LeetCode】060. Permutation Sequence

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

  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] Permutation Sequence 序列排序

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

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

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

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

随机推荐

  1. Linux_异常_02_WinSCP上传文件时显示Permission denied

    异常现象如下: 二.解决方案 1.设置对应目录权限全开,就可以上传文件到这个目录了 sudo chmod 777 /devloper

  2. leetcode 304. Range Sum Query 2D - Immutable(递推)

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  3. TF-IFD算法及python实现关键字提取

    TF-IDF算法: TF:词频(Term Frequency),即在分词后,某一个词在文档中出现的频率. IDF:逆文档频率(Inverse Document Frequency).在词频的基础上给每 ...

  4. 集训Day9

    又是不想学化学但元气满满的一天呢qwq 今天又有新的故事发生那就是! bzoj2150 最小流 每个点拆成$a_x$和$a_y$ $S$->$a_x$容量为1 $a_y$->$T$容量为1 ...

  5. poj3784 Running Median[对顶堆]

    由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...

  6. No overload for 'OnStartup' matches delegate 'System.Windows.StartupEventHandler'

    No overload for &apos;OnStartup&apos; matches delegate &apos;System.Windows.StartupEvent ...

  7. xpath技术,用在dom4j中

    title: xPath语法应用 tags: xPath,dom4j grammar_cjkRuby: true --- 在dom4j中,会使用到xPath技术. 在项目中导入 jaxen-1.1-b ...

  8. centos7 install pip

    1. 安装过程 yum -y install epel-release yum install python-pip pip install --upgrade pip

  9. 浅谈HTML移动Web开发(转)

    一.响应式Web设计 PC端常用的两种布局方式就是固定布局和弹性布局,前者设置一个绝大多数电脑能征服显示的固定宽度居中显示,后者则采用百分百. 响应式布局意味着媒体查询,响应式web设计并非新的技术, ...

  10. linux正则表达式基础

    linux中awk,sed,grep等 命令使用区别正则表达式基础 在最简单的情况下,一个正则表达式看上去就是一个普通的查找串.例如,正则表达式"testing"中没有包含任何元字 ...