LeetCode: Permutation Sequence 解题报告
Permutation Sequence
https://oj.leetcode.com/problems/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.
解答:
1. 以某一数字开头的排列有(n-1)! 个。
例如: 123, 132, 以1开头的是 2!个
2. 所以第一位数字就可以用 (k-1) / (n-1)! 来确定
.这里K-1的原因是,序列号我们应从0开始计算,否则在边界时无法计算。
3. 第二位数字。假设前面取余后为m,则第二位数字是 第 m/(n-2)! 个未使用的数字。
4. 不断重复2,3,取余并且对(n-k)!进行除法,直至计算完毕
以下为主页君的代码,敬请指正:
解法1:
采用比较复杂的boolean来计算数字的索引(我们需要用一个boolean的数组来记录未使用的数字):
package Algorithms.permutation; /*
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.
* */
public class PermutationSequence {
public static String getPermutation(int n, int k) {
if (n == ) {
return "";
} // 先计算出(n)!
int num = ;
for (int i = ; i <= n; i++) {
num *= i;
} boolean[] use = new boolean[n];
for (int i = ; i < n; i++) {
use[i] = false;
} // 因为index是从0开始计算
k--;
StringBuilder sb = new StringBuilder();
for (int i = ; i < n; i++) {
// 计算完第一个数字前,num要除以(n)
num = num / (n - i); int index = k / num;
k = k % num; for (int j = ; j < n; j++) {
if (!use[j]) {
if (index == ) {
// 记录下本次的结果.
sb.append((j + ) + "");
use[j] = true;
break;
} // 遇到未使用过的数字,记录index
index--;
}
}
} return sb.toString();
} public static void main(String[] args) {
System.out.println(getPermutation(, ));
} }
解法2:
优化后,使用链表来记录未使用的数字,每用掉一个,将它从链表中移除即可。
public String getPermutation1(int n, int k) {
// 1:17 -> 1:43
LinkedList<Character> digits = new LinkedList<Character>();
// bug 2: should only add n elements.
for (char i = ''; i <= '' + n; i++) {
digits.add(i);
}
k = k - ;
StringBuilder sb = new StringBuilder();
int sum = ;
// n!
for (int i = ; i <= n; i++) {
sum *= i;
}
int cur = n;
while (!digits.isEmpty()) {
sum /= cur;
cur--;
int digitIndex = k / sum;
k = k % sum;
//Line 25: error: cannot find symbol: method digits(int)
sb.append(digits.get(digitIndex));
// remove the used digit.
digits.remove(digitIndex);
}
return sb.toString();
}
解法3:
在2解基础进一步优化,使用for 循环替代while 循环,更简洁:
public String getPermutation(int n, int k) {
// 1:17 -> 1:43
LinkedList<Character> digits = new LinkedList<Character>();
// bug 2: should only add n elements.
for (char i = ''; i <= '' + n; i++) {
digits.add(i);
}
// The index start from 0;
k--;
StringBuilder sb = new StringBuilder();
int sum = ;
// n!
for (int i = ; i <= n; i++) {
sum *= i;
}
for (int i = n; i >= ; i--) {
sum /= i;
int digitIndex = k / sum;
k = k % sum;
//Line 25: error: cannot find symbol: method digits(int)
sb.append(digits.get(digitIndex));
// remove the used digit.
digits.remove(digitIndex);
}
return sb.toString();
}
LeetCode: Permutation Sequence 解题报告的更多相关文章
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- [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 - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- 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] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- js数据结构之二叉树的详细实现方法
数据结构中,二叉树的使用频率非常高,这得益于二叉树优秀的性能. 二叉树是非线性的数据结构,用以存储带有层级的数据,其用于查找的删除的性能非常高. 二叉树 数据结构的实现方法如下: function N ...
- Web前端性能优化——编写高效的JavaScript
前言 随着计算机的发展,Web富应用时代的到来,Web 2.0早已不再是用div+css高质量还原设计的时代.自Gmail网页版邮件服务的问世开始,Web前端开发也开启了新的纪元.用户需求不断提高,各 ...
- Codeforces.567E.President and Roads(最短路 Dijkstra)
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
- 洛谷P1265 公路修建(Prim)
To 洛谷.1265 公路修建 题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成. 修建工程分若干轮完 ...
- 潭州课堂25班:Ph201805201 第五课:格式化输出和深浅复制 (课堂笔记)
格式化输出和字符串转义 占位符 使用示意 作用 %s '%s %s' % ('hello', 'world') 表示占位的是str %d '%d %d' % (1, 2) 表示占位的是int %d ' ...
- Codeforces Round #109 (Div. 1) 题解 【ABC】
A - Hometask 题意:给你一个字符串,然后再给你k个禁止挨在一起的字符串,问你最少删除多少个字符串,使得不会有禁忌的字符串对挨在一起.题目保证每个字符最多出现在一个禁忌中. 题解:由于每个字 ...
- spring源码分析系列
spring源码分析系列 (1) spring拓展接口BeanFactoryPostProcessor.BeanDefinitionRegistryPostProcessor spring源码分析系列 ...
- OEMbutton乱码问题解决
一.出现故障: 在Linux环境中安装tid=12">Oracle 10g,启用EM时.出现button显示乱码现象,例如以下: 二.分析问题: 由于在安装Oracle10g时,JDK ...
- 最近IOS10.2.1 iphone6 无法通过appStore 来更新 下载任何APP。好烦啊。
今天打开爱思助手,查看事实日志,发现了原因: Mar :: iPhone6 appstored(StoreServices)[] <Notice>: SSSQLiteDatabase: C ...
- 奇怪吸引子---LuChen
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...