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, ...
随机推荐
- boost--文件、目录操作
filesystem库是文件系统操作库,可以使用其中的basic_path类用来操作目录.文件,使用需要包含编译好的system库和filesystem库,我们一般不直接使用basic_path,而是 ...
- Greenplum 日常维护手册 (汇总、点评、备查)
1. 数据库启动:gpstart常用可参数: -a : 直接启动,不提示终端用户输入确认-m:只启动master 实例,主要在故障处理时使用2. 数据库停止:gpstop:常用可参数:-a:直接停止, ...
- spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程
RequestMappingHandlerMapping getMappingForMethod RequestMappingHandlerMapping 继承于 AbstractHandlerMet ...
- linux下使用iptables统计ip/端口流量
1.添加ip/端口的流量统计 入网流量: iptables -A INPUT -d 出网流量: iptables -A OUTPUT -s 2.查看流量统计信息 iptables -L -v -n - ...
- junit 方法:assertEquals 和 assertTrue
assertEquals 和 assertTrue 区别相同之处:都能判断两个值是否相等 assertTrue 如果为true,则运行success,反之Failure assertEquals 如果 ...
- C语言 字符二维数组(多个字符串)探讨 求解
什么是二维字符数组? 二维字符数组中为什么定义字符串是一行一个? “hello world”在C语言中代表什么? 为什么只能在定义时才能写成char a[10]="jvssj" ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- SpringMVC拦截静态资源的处理办法
SpringMVC拦截静态资源导致 JS CSS 无法加载 可以在配置文件中加入以下代码 <mvc:resources location="/statices/" m ...
- python+unittest 控制用例的执行顺序
unittest的main()方法执行用例的顺序是按照测试类.测试方法的名字的ASCII顺序来执行测试方法.所以可能执行的顺序和你想要的顺序不一样,可能通过下面两种方法修改执行顺序 1. 通过Test ...
- weblogic.xml 精妙设置
1.一:weblogic 页面访问速度比tomcat慢的原因和解决办法 一:weblogic 页面访问速度比tomcat慢的原因和解决办法 公司有个项目,部署在weblogic8.1上之后,发现比在t ...