Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2cff2ea

Browse filesBrowse files
author
王俊超
committed
commit
1 parent b60d789 commit 2cff2ea
Copy full SHA for 2cff2ea

File tree

Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2018-09-28 15:45
7+
**/
8+
public class Solution {
9+
public String getPermutation(int n, int k) {
10+
11+
StringBuilder sb = new StringBuilder();
12+
List<Integer> num = new ArrayList<>();
13+
// 用于计录最大的阶乘数
14+
int fact = 1;
15+
for (int i = 1; i <= n; i++) {
16+
fact *= i;
17+
num.add(i);
18+
}
19+
20+
// l = k - 1;为了方便从0开始计数
21+
int index;
22+
for (int i = 0, l = k - 1; i < n; i++) {
23+
// 从最大的阶乘数开始减少
24+
fact /= (n - i);
25+
// 计算剩下的数据中的索引
26+
index = (l / fact);
27+
// 将索引值添加到结果中
28+
sb.append(num.remove(index));
29+
// 余下要处理的数
30+
l -= index * fact;
31+
}
32+
return sb.toString();
33+
}
34+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.