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 ...
随机推荐
- C++ Const引用详解
(1) 在实际的程序中,引用主要被用做函数的形式参数--通常将类对象传递给一个函数.引用必须初始化. 但是用对象的地址初始化引用是错误的,我们可以定义一个指针引用. 1 int ival ...
- CSS 补充
属性选择器下面的例子为带有 title 属性的所有元素设置样式:[title]{ color:red;} <h1>可以应用样式:</h1><h2 title=" ...
- 编译CDH Spark源代码
如何编译CDH Spark源代码 经过漫长的编译过程(我编译了2个半小时),最终成功了,在assembly/target/scala-2.10目录下面有spark-assembly-1.0.0-cdh ...
- Mac Pro 开机自启动 PHP-FPM,Nginx,MySql 等软件
在Mac下安装好了PHP开发环境(PHP-FPM,Nginx,MySql), 想设置成开机自启动,原来以为和一般的Linux系统一样,也是在rc.d这样目录放置启动脚本.在网上查了一些资料,发现苹果应 ...
- mysql索引优化
mysql 索引优化 >mysql一次查询只能使用一个索引.如果要对多个字段使用索引,建立复合索引. >越小的数据类型通常更好:越小的数据类型通常在磁盘.内存和CPU缓存中都需要更少的空间 ...
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- 带你走进rsync的世界
导读 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录.rsync共有3种使用方 ...
- 关于tableView的错误提示
WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWit ...
- linux下编译qt5.6.0静态库——configure配置
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- django foreign key 自动加_id问题
解决:http://stackoverflow.com/questions/8223519/preventing-django-from-appending-id-to-a-foreign-key-f ...