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 ...
随机推荐
- 必须知道的.net(继承)
1.继承定义:就是面向对象中类与类之间的一种关系.通过继承,使得子类具有父类的属性和方法,同时子类也可以通过加入新的属性和方法或者修改父类的属性和方法建立新的类层次. 2.CLR支持实现单继承和接口多 ...
- 【C语言入门教程】4.2 二维数组
C 语言允许使用多维数组,即使用多组小标的数组,二维数组是最常用的多维数组.多维数组在内存中存放数据的顺序与一维数组相同,使用连续的存储单元. 4.2.1 二维数组的一般形式 二维数组的一般声明形式为 ...
- 软件测试-----Graph Coverage作业
/******************************************************* * Finds and prints n prime integers * Jeff ...
- mysql-mysql悲观锁和乐观锁
1.mysql的四种事务隔离级别 I. 对于同时运行多个事务,当这些事务访问数据库中的相同数据时,如果没有采取必要的隔离机制,就会导致各种并发问题. (1)脏读: 对于两个事物 T1, T2, T1 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- static 的使用
static用法小结 转自 http://blog.csdn.net/Kendiv/article/details/675941 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...
- $.extend()了解心得
2.1 extend(result,item1,item2-..) 这里这个方法主要用来合并,将所有的参数项都合并result中,并返回result,但是这 样就会破坏result的结构. 2.2 e ...
- git 教程(7)--撤销修改
自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行:
- Android 创建内容提供器(ContentResolver)
如果想实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 ContentResolver 的方式来创建一个自己的内容提供器. ContentProvider 类中有六 ...