当前位置:网站首页>Leetcode60. permutation sequence
Leetcode60. permutation sequence
2022-07-29 00:01:00 【Java full stack R & D Alliance】
Title transmission address :https://leetcode.cn/problems/permutation-sequence/
Let's first show you a recursive solution using full permutation , But it does not meet the performance requirements of the topic .
Code :
public static String getPermutation(int n, int k) {
String[] allArrange = getAllArrange(n);
Arrays.sort(allArrange);
return allArrange[k - 1];
}
/** * Full Permutation * * @param n * @return */
public static String[] getAllArrange(int n) {
if (n == 1) {
String[] res = new String[1];
res[0] = "1";
return res;
}
List<String> list = new ArrayList<>();
String[] allArrange = getAllArrange(n - 1);
for (int i = 0; i < allArrange.length; i++) {
String str = allArrange[i];
for (int j = 0; j <= str.length(); j++) {
StringBuilder stringBuilder = new StringBuilder(str);
stringBuilder.insert(j, n);
list.add(stringBuilder.toString());
}
}
String[] res;
res = list.toArray(new String[0]);
return res;
}
The really powerful code is as follows
public static String getPermutation(int n, int k) {
StringBuilder stringBuilder = new StringBuilder();
while (n > 0) {
stringBuilder.insert(0, n);
n--;
}
// such as n=4, be result=1234
return getResult(stringBuilder.toString(), k);
}
public static String getResult(String str, int k) {
// Dealing with border situations
if (str.length() == 1) {
return str;
}
int index = 0;
int factorial = doFactorial(str.length() - 1);
// Calculate the first character // such as n=4,k=9 Under the circumstances , The correct answer to this question should be "2314", factorial=6,
// and 9/6=1, Go through the following while After the cycle ,fisrtChar=2, k=3
while (k > factorial) {
index++;
k -= factorial;
}
if (k == 0 && index == 0) {
return str;
}
char firstChar = str.charAt(index);
String res = new StringBuilder(str).deleteCharAt(index).toString();
String result = getResult(res, k);
StringBuilder stringBuilder = new StringBuilder(result);
stringBuilder.insert(0, firstChar);
return stringBuilder.toString();
}
/** * Factorial * @param m * @return */
public static int doFactorial(int m) {
if (m == 1) {
return 1;
}
return doFactorial(m - 1) * m;
}
Operational efficiency :
边栏推荐
- Websocket heartbeat mechanism (keep alive mechanism)
- VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」
- 1-7 解决类中方法的this指向问题
- [data mining engineer - written examination] Dahua shares in 2022
- DevOps在物联网解决方案中的应用
- 数仓:Doris在美团的应用实践
- 脲酶丨Worthington杰克豆脲酶的特性及测定方案
- GhostNets on Heterogeneous Devices via Cheap Operations
- Kingbasees client programming interface guide ODBC (4. Create data source)
- Worthington丨Worthington胰蛋白酶抑制剂说明书
猜你喜欢

GhostNets on Heterogeneous Devices via Cheap Operations

Genomic DNA isolation Worthington ribonuclease A

Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan

Worthington丨Worthington胰蛋白酶抑制剂说明书

Eight performance analysis indicators of effective supply chain management (Part 1)

SAP 临时表空间错误处理

使用Pytorch快速训练网络模型

EN 1935 building hardware. Single axis hinge - CE certification

Leetcode64. 最小路径和

Linux之yum安装MySQL
随机推荐
Eight performance analysis indicators of effective supply chain management (Part 1)
Worthington丨Worthington胰蛋白酶化学性质及相关研究
数据中台的那些“经验与陷阱”
Interpretation of ISO 13400 (doip) standard
VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」
Pycharm new project
PowerCL 批量创建及管理虚拟交换机
pycharm配置运行环境
Doip communication of canoe application case
【MySQL 8】Generated Invisible Primary Keys(GIPK)
PMP Exam countdown, look at 3A pass bag!
1-4 类的复习
Compose 的声明式代码如此简洁?
Websocket heartbeat mechanism (keep alive mechanism)
软件设计师的错题汇总
Pycharm configuring the running environment
leetcode 763. Partition Labels 划分字母区间(中等)
Leetcode60. 排列序列
Oracle创建表空间和用户
1-7 解决类中方法的this指向问题