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, ...
随机推荐
- redis学习-列表(list)常用命令
redis学习-列表(list)常用命令 lpush:从列表左侧头部添加数据 rpush:从右侧尾部添加数据 lpop:从给左侧头部取出一个元素 rpop:从右侧尾部取出一个元素 lrange:取 ...
- 现网环境业务不影响,但是tomcat启动一直有error日志,ERROR org.apache.catalina.startup.ContextConfig- Unable to process Jar entry [module-info.class] from Jar [jar:file:/home/iufs/apache-tomcat/webapps/iufs/WEB-INF/lib/asm
完整的错误日志信息: 2019-03-19 15:30:42,021 [main] INFO org.apache.catalina.core.StandardEngine- Starting Ser ...
- 将Paul替换成Ringo
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 用Python对html进行编码
原地址:https://stackoverflow.com/questions/275174/how-do-i-perform-html-decoding-encoding-using-python- ...
- CNN算法解决MNIST数据集识别问题
网络实现程序如下 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 用于设置将记 ...
- JavaScriptDOM
DOM简介 1.HTML DOM:网页被加载时,浏览器会创建文档对象模型 2.DOM操作HTML:改变HTML的元素.属性.CSS样式.对所有事件作出反应 DOM操作HTML 1.改变HTML输出流 ...
- 个人对于angularjs依赖注入的理解
依赖注入(Dependency Injection,DI),作者认为本文中所有名词性的"依赖" 都可以理解为 "需要使用的资源". 对象或者函数只有以下3种获取 ...
- 【repost】CSS3弹性布局
本文导读:在CSS 3中,CSS Flexible Box模块为一个非常重要的模块,该模块用于以非常灵活的方式实现页面布局处理.使用CSS Flexible Box模块中定义的弹性盒布局技术,可以根据 ...
- 我所理解Java集合框架的部分的使用(Collection和Map)
所谓集合,就是和数组类似——一组数据.java中提供了一些处理集合数据的类和接口,以供我们使用. 由于数组的长度固定,处理不定数量的数据比较麻烦,于是就有了集合. 以下是java集合框架(短虚线表示接 ...
- Html/CSS前端如何实现文字边框阴影
上次我们聊了<Html5前端如何实现文字阴影>,其实在开发中现在对于阴影效果的使用已经越来越广泛了,那么今天我们就来说一说用同样的手法实现边框阴影. 一.边框阴影box-shadow 边框 ...