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 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.
解题思路:
之前曾在Java for LeetCode 046 Permutations和Java for LeetCode 031 Next Permutation做过关于字典排序的算法,但是可以肯定,这种近乎暴力枚举的算法肯定是通不过测试的!通过观察发现k / factorial(n - 1)其实就代表了str的第一个字母,顺着这个思路以此类推即可,JAVA实现如下:
static public String getPermutation(int n, int k) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < n; i++)
str.append(i + 1);
StringBuilder sb = new StringBuilder();
while (n > 1) {
int index = k / factorial(n - 1);
k %= factorial(n - 1);
if(k==0){
index--;
sb.append(str.charAt(index));
str.deleteCharAt(index);
sb.append(new StringBuilder(str).reverse());
return sb.toString();
}
sb.append(str.charAt(index));
str.deleteCharAt(index);
n--;
}
return "1";
}
static int factorial(int n) {
if (n == 1)
return 1;
else
return (factorial(n - 1) * n);
}
Java for LeetCode 060 Permutation Sequence的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【LeetCode】060. 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 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 the p ...
- 【leetcode】 Permutation Sequence (middle)
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 之 Permutation Sequence
Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【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 (排列序列) 解题思路和方法
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析
上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种 ...
- MyEclipse10中导入的jquery文件报错(出现红叉叉,提示语法错误)
为了做一个页面特效,导入了一个jQuery文件,怎想,myeclipse竟然报错说是语法错误,但是这个js文件我是从官网上下载的,不应该出错才对,百度谷歌之后终于找到了解决办法: 选中报错的js文件, ...
- 【poj2187】 Beauty Contest
http://poj.org/problem?id=2187 (题目链接) 题意 求点集上两点间最长距离 Solution 凸包+旋转卡壳. 旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要 ...
- [转]基于overlayfs的硬盘资源隔离工具troot
原文在这里:http://blog.donghao.org/tag/overlayfs/ 某些开发测试团队会有这样的需求:多个开发或测试人员在一台物理机上搭环境.装rpm包.测试等,目录很可能互相干扰 ...
- this的使用
1.使用this调用本类中的属性 class Person{ private String name; private int age; public Person(String name,int a ...
- hihocoder #1285 智力竞赛
传送门 总结: 1.仔细读题 2.仔细分析复杂度 3.不要想当然,乱下结论 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队 ...
- ajax入门详解
l 一个实例 在开始正式讲解 Ajax之前,首先让我们先来看看Google Map使用Ajax改善其产品设计的效果. 1. 在浏览器地址栏中输入http://maps.google.com打开Goog ...
- iOS开发的那些坑
最近重新拿起了iOS的开发,使用OC和Swift混编,碰到了一些比较棘手的问题,在这里记录下来,方便自己以后或他人不再入坑.这篇文章的内容包含: UITableViewCell的真实结构在iOS的环境 ...
- C++ map 映照容器
map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系. map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较, ...
- Java初学(二)
一.数据类型 在定义Long或者Float类型变量的时候,要加L或f(大小写无关,只是便于识别,建议不要小写L) 整数默认是int,浮点数默认是double 二.java字符 java语言采用的是Un ...