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 ...
随机推荐
- 【CodeForces 606A】A -特别水的题1-Magic Spheres
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/A Description Carl is a beginne ...
- BZOJ4590 自动刷题机
Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...
- 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
在部署到IIS7时,MVC3报了一个找不到资源的错误,文件肯定是有的,而且页面是肯定报错的,也就说内部运行错误了,而MVC把错误没有抛出来而已: 所以对症下药,发觉我的项目里面用了rexs进行多语言, ...
- groovy–流程控制
在本篇文章中,我们将介绍逻辑分支,循环,以及如何从if-else以及try-catch代码块中返回值. if – elseGroovy 支持Java传统的if-else语法: def x = fals ...
- HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- android button 函数调用栈
Button button=(Button) findViewById(R.id.button);button.setOnClickListener(new Button.OnClickListene ...
- CentOS 6.4安装Apache+MySQL+PHP的图文教程
LAMP 实际上就是 Linux.Apache.MySQL.PHP 四个名称的缩写,当然最后一个 “P” 还有其他说法是 Perl 或者 Python.不用多说了,本文讲解的就是 Linux.Apac ...
- 线性判别分析(LDA)准则:FIsher准则、感知机准则、最小二乘(最小均方误差)准则
准则 采用一种分类形式后,就要采用准则来衡量分类的效果,最好的结果一般出现在准则函数的极值点上,因此将分类器的设计问题转化为求准则函数极值问题,即求准则函数的参数,如线性分类器中的权值向量. 分类器设 ...
- js闭包理解
js闭包的作用是使函数外可以访问函数内部的变量,是通过 在函数内部 定义 访问函数内变量 的函数实现的,内部的一个函数产生一个闭包 function a() { var i=0; return fun ...
- Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang
Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang Recruit Ponpare is Japan's leading ...