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 ...
随机推荐
- Chrome商店Crx离线安装包下载
第一步:找到Chrome的扩展应用ID 第二步:输入扩展应用ID 第三步:单击 生成 按钮. 第四步:在这里右键另存为即可下载.
- HDU-1754I Hate It 线段树区间最值
这道题比较基本,就是用线段树维护区间最值,可以算是模板吧-.. I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768 ...
- 【uoj150】 NOIP2015—运输计划
http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...
- QA要懂的Linux命令
<一>软件安装相关QA经常需要安装测试软件(jmeter.Mock.python环境搭建.java环境搭建),或者配置测试环境(nginx.ci等),需要了解linux下如何安装软件.在工 ...
- unity 全屏乱影 BlitMultiTap
http://m.blog.csdn.net/blog/stalendp/40859441 官方例子AngryBots的链接地址:http://u3d.as/content/unity-technol ...
- afasf
http://www.cnblogs.com/ttzhang/archive/2008/11/02/1324601.html project server 2007 sn :W2JJW-4KYDP-2 ...
- jquery autocomplete 简单实用例子
<link href="../../themes/default/css/jquery.ui.all.css" rel="stylesheet" type ...
- gvim设置成不备份文件
打开gVim,进入“编辑”-“启动设定” 在“behave mswin”下行位置添加 set nobackup 语句 退出并保存配置文件 :wq
- logback 项目应用
1.gradle引用: compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3' compile grou ...
- editplus快捷键大全之editplus编辑快捷键
前面我们说了editplus快捷键大全之editplus文件快捷键和editplus快捷键大全之editplus光标快捷键,这里我们讲一下editplus快捷键大全之editplus编辑快捷键 删除光 ...