leetcode — permutation-sequence
import java.util.ArrayList;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/permutation-sequence/
*
*
* The set [1,2,3,…,n] contains a total of n! unique permutations.
*
* By listing and labeling all of the permutations in order,
* We get the following sequence (ie, for n = 3):
*
* "123"
* "132"
* "213"
* "231"
* "312"
* "321"
*
* Given n and k, return the kth permutation sequence.
*
* Note: Given n will be between 1 and 9 inclusive.
*/
public class PermutationSequence {
/**
* 寻找n个元素组成的第k个排列
*
* 第一方法:
* 从最小的开始,循环k次找到第k个排列
*
* 第二种方法:
* 找到第k个排列的规律
*
* 这种题目,可以先以一个简单的例子来找规律
* 这里假设n = 4, k = 9
* 1234
* 1243
* 1324
* 1342
* 1423
* 1432
* 2134
* 2143
* 2314 >>>> k = 9
* 2341
* 2413
* 2431
* 3124
* 3142
* 3214
* 3241
* 3412
* 3421
* 4123
* 4132
* 4213
* 4231
* 4312
* 4321
*
* 以下认为arr下标从1开始
* 第一位:
* arr[4] = {1,2,3,4},k = 9,n = 4
* 上面的排列是有规律的,每个位置每一个数字出现cycle = (n-1) = (4-1)! = 6次,整体第k个的时候,该位置已经经过 count = k / cycle = 1个周期,则该位置的数是arr[count + 1] = 2,因为已经过了count个周期,是第count+1个周期,就轮到了数组arr中的第count+1个数字
*
* 第二位:
* arr[3] = {1,3,4},k' = k % (n-1)! = 3,n' = 3
* 右面一个位置的数字,以2开头,因为2已经确定,剩下的形成一个新的数组arr[3] = {1,3,4},每个位置每个数字出现cycle = (n' - 1) = (3-1)! = 2次,整体第k个的时候,对于当前数组组成的排列而言处于第k' = k % (n-1) = 3个,第三个的第一位这个时候已经经过count = k' / cycle = 1个周期,正处于第index = count + 1 = 2个周期,也就是该位置的数为arr[index]
*
* 第三位:
* arr[2] = {1,4},k'' = k' % (n'-1)! = 1,n'' = 2
* cycle = (n'' - 1)! = 1, count = k'' / cycle = 1,index = count + 1 = 2, arr[index] = 4
*
* 第四位:
* arr[1] = {1}, k''' = k'' % (n'' - 1)! = 0, n''' = 1
* cycle = (n''' - 1)! = 1,index = k''' / cycle + 1 = 1,arr[index] = 1
*
* @param n
* @param k
* @return
*/
public String getPermutation (int n, int k) {
if (n < 1) {
return "";
}
int[] fatorial = new int[n];
List<Integer> arr = new ArrayList<Integer>();
fatorial[0] = 1;
arr.add(1);
for (int i = 1; i < n; i++) {
fatorial[i] = fatorial[i-1] * i;
arr.add(i+1);
}
int index = 0;
// 因为数组下标是从0开始的,k--就是为了让数组下标从0开始
k--;
StringBuilder result = new StringBuilder();
while (n > 0) {
index = k / fatorial[n-1];
result.append(arr.get(index));
arr.remove(index);
k = k % fatorial[n-1];
n--;
}
return result.toString();
}
public static void main(String[] args) {
PermutationSequence permutationSequence = new PermutationSequence();
System.out.println(permutationSequence.getPermutation(4, 9));
System.out.println(permutationSequence.getPermutation(4, 24));
}
}
leetcode — permutation-sequence的更多相关文章
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- LeetCode——Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Leetcode] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- 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 ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
随机推荐
- Maven之pom.xml配置文件详解
此文非原创,摘自:https://www.baidu.com/link?url=GlGgW21nijIiULDZj0RfPH8ofqGMqEnAzXiym7O3hfrZM5nFH2enukemBNTX ...
- Linux 第十六天
十六.服务管理 1.服务分类 1)RPM包默认安装的服务:包括独立的服务.基于xinetd的服务 2)源码包安装的服务 3)RPM安装服务和源码包安装服务的区别就是安装位置的不同 >源码包安装在 ...
- Win10专业版激活
转载来自:http://www.zhuangjiba.com/bios/3432.html 如何激活win10正式版图文解说 打开开始菜单,找到设置,点开“更新和安全”,切换到“激活”选项卡,查看到当 ...
- centos 下Python独立虚拟环境创建
virtualenv Python有着庞大的开源社区的支持,很自然就产生这么一个问题:第三方包参差不齐,如果我们想在服务器测试,或者升级某个包,就会导致生产环境产生杂乱,多余的第三方依赖包. virt ...
- nice coding (与其亡羊补牢,不如未雨绸缪)
一.需求前 架构规范 建模规范 编码规范(流程控制,方法抽取,日志打印等) <Effective Java> <Design Patterns> 二.需求中 1. 明确需求(别 ...
- opencv2.4.13+python2.7学习笔记--OpenCV中的图像处理--图像轮廓特征和几何矩
阅读对象:对概率论中的期望有一点了解. 1.图像几何矩 1.1简述 图像的几何矩包括空间矩.中心矩和中心归一化矩.几何矩具有平移.旋转和尺度不变性,一般是用来做大粒度的区分,用来过滤显然不相关的图像. ...
- qscoj 喵哈哈村的打印机游戏 区间dp
点这里去看题 区间dp ,dp[l][r][d]代表从l到r的区间底色为d,具体看代码 第一次见到区间dp...两个小时对着敲了五遍终于自己敲懂了一遍ac #include<bits/stdc+ ...
- Java实现zip压缩文件的解压
需求描述: 前段时间写了一篇博客<Java实现对文本文件MD5加密并ftp传送到远程主机目录>,实现了一部分的业务需求.然而有些业务可能不止传送一个文件,有时候客户需要传多个文件,原有系统 ...
- Java程序CPU使用率过高
Java程序CPU使用率过高 通过top命令找到使用率过高的java进程PID 根据进程号查找线程TID:ps -mp PID -o THREAD,tid,time 将TID转换成16进制:print ...
- Docker构建其它组件
构建mysql 运行centos7容器 docker run --privileged -dti --name=centos-container centos:7 /usr/sbin/init 查询c ...