60. 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.
给定n,排列成n位数,会有n!种组合,按大小排列,输出第k个数的值。
代码:
该题目看起来就不是那么复杂,但是是medium的,说明把所有的数字排出来,排序,肯定是不行的。
于是,观察规律,肯定要先确定最高位的数字。1-n无论哪一个数字在最高位,都对应(n-1)!个组合的数字。
当k>(n-1)!且k<2*(n-1),说明第一位数字是2,因为1开头的排完了,也没有排到K,但也不会比两个(n-1)!大,所以首位可以确定。
当k<(n-1)!,自然首位就是剩余元素中最小的那个。比如一开始1-n,自然就是1了。
根据该规律,分情况,递归求出每次剩余元素中应该放在首位的那个,用链表记录1-n个元素,方便删除操作,首位用栈记录(方便):
java代码,不难理解,但还是试了半天,哎。。。:
//递归求阶乘
public int factorial(int n) {
if(n>1) {
n = n*factorial(n-1);
}
return n;
}
//从首位开始,递归入栈每一位对应元素
ArrayDeque<Integer> stack=new ArrayDeque<Integer>();
public void getFirstNum(List<Integer> num,int k) {
int i = 1;
int n=num.size();
int temp = factorial(n-1);
//每次当n为1的时候,只有一个元素了,直接入栈并退出函数
if(n==1) {
stack.push((Integer) num.get(0));
System.out.println("入栈: "+(Integer) num.get(0));
return;
}
//k小于(n-1)!,所以直接取链表中最小的数为首位,入栈
if(temp >=k) {
stack.push((Integer) num.get(0));
System.out.println("入栈: "+(Integer) num.get(0));
num.remove(0);
getFirstNum(num,k);
}
else {
//k大于(n-1)!,循环找出k大于几个(n-1)!
while (i*temp < k){
i++;
//k大于i个(n-1)!,取链表中第i个位置对应的数为首位,入栈
if(i*temp >= k) {
stack.push((Integer) num.get(i-1));
System.out.println("入栈: "+(Integer) num.get(i-1));
num.remove(i-1);
k = k-(i-1)*factorial(n-1);
getFirstNum(num,k);
break;
}
}
}
}
//获得相应位置的排列
public String getPermutation(int n, int k) {
if(n==0){return null;}
int result_int = 0;
String result_str = null;
ArrayList<Integer> num = new ArrayList<Integer>(n);
for (int j=1;j<=n;j++) {
num.add(j);
}
getFirstNum(num,k);
while(!stack.isEmpty()) {
result_int= result_int*10+ stack.pollLast();
}
System.out.println("第"+k+"元素是: "+result_int);
result_str = String.valueOf(result_int);
return result_str;
}
结果:
60. Permutation Sequence的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 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 ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode OJ 60. Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...
- 60. Permutation Sequence (String; Math)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 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 ...
随机推荐
- Redis安装和使用
新年伊始,万象更新.祝大家:新的一年,工作顺利,生活越来越美好. http://www.redis.cn/commands.html http://try.redis.io/ http://www.c ...
- java遍历map的四种方式
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 第11天 Stack Queue
1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...
- 关于outerWidth()属性
在写代码的时候,获取元素的宽度通常用到这个属性.此属性具有如下特点: 1.默认情况下,它的值为所有后代元素(含此元素本身)中最大的宽度值. 2.若某后代元素的display属性为none,那么在计算的 ...
- ubuntu安装python一些安装包
sudo apt-get install python-pip sudo pip install distribute sudo pip install nose sudo pip install v ...
- leetcode150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- JavaScript深入浅出1-数据类型
慕课网教程视频地址:Javascript深入浅出 javascript是弱数据类型语言,不需要显式的定义类型,一共有如下六种数据类型 原始类型:number string boolean null u ...
- Android_bug之Default Activity not found
在运行自己修改默认的MainActivety,运行自己的Mainactivety时,碰到这个问题Could not identify launch activity: Default Activity ...
- java File delete()执行失败原因
java.io.File里的delete操作很实用也很常用,可以用来删除单独的文件和某一目录.但有时候会出现delete失败的情况,出现这种情况的原因一般有以下几种: 1.删除时还有其他程序在使用该文 ...
- [转]Python的ASCII, GB2312, Unicode , UTF-8
2007-12-13 10:50:47| 分类: Python实用软件编|举报|字号 订阅 ASCII 是一种字符集,包括大小写的英文字母.数字.控制字符等,它用一个字节表示,范围是 0-1 ...