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

Latest commit

 

History

History
History
69 lines (55 loc) · 1.7 KB

File metadata and controls

69 lines (55 loc) · 1.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 == 0) {
return "";
}
// 先计算出(n)!
int num = 1;
for (int i = 1; i <= n; i++) {
num *= i;
}
boolean[] use = new boolean[n];
for (int i = 0; i < n; i++) {
use[i] = false;
}
// 因为index是从0开始计算
k--;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
// 计算完第一个数字前,num要除以(n)
num = num / (n - i);
int index = k / num;
k = k % num;
for (int j = 0; j < n; j++) {
if (!use[j]) {
if (index == 0) {
// 记录下本次的结果.
sb.append((j + 1) + "");
use[j] = true;
break;
}
// 遇到未使用过的数字,记录index
index--;
}
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(getPermutation(3, 5));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.