Skip to content

Commit 00f3f1e

Browse files
committed
60. Permutation Sequence
1 parent caedf40 commit 00f3f1e

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

markdown/60. Permutation Sequence.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### [60\. Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
The set `[1,2,3,...,_n_]` contains a total of _n_! unique permutations.
7+
8+
By listing and labeling all of the permutations in order, we get the following sequence for _n_ = 3:
9+
10+
1. `"123"`
11+
2. `"132"`
12+
3. `"213"`
13+
4. `"231"`
14+
5. `"312"`
15+
6. `"321"`
16+
17+
Given _n_ and _k_, return the _k_<sup>th</sup> permutation sequence.
18+
19+
**Note:**
20+
21+
* Given _n_ will be between 1 and 9 inclusive.
22+
* Given _k_ will be between 1 and _n_! inclusive.
23+
24+
**Example 1:**
25+
26+
```
27+
Input: n = 3, k = 3
28+
Output: "213"
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: n = 4, k = 9
35+
Output: "2314"
36+
```
37+
38+
39+
#### Solution
40+
41+
Language: **Java**
42+
43+
```java
44+
class Solution {
45+
   public String getPermutation(int n, int k) {
46+
       int[] factorial = new int[n + 1];
47+
       List<Integer> leftNumList = new ArrayList<>();
48+
       int sum = 1;
49+
       factorial[0] = 1;
50+
       for (int i = 1; i <= n; i++) {
51+
           leftNumList.add(i);
52+
           sum *= i;
53+
           factorial[i] = sum;
54+
      }
55+
       StringBuilder stringBuilder = new StringBuilder();
56+
       k--; // Amazing k--
57+
       for (int i = 1; i <= n; i++) {
58+
           int index = k / factorial[n - i];
59+
           k = k % factorial[n - i];
60+
           stringBuilder.append(leftNumList.get(index));
61+
           leftNumList.remove(index);
62+
      }
63+
       return stringBuilder.toString();
64+
  }
65+
}
66+
```
67+
![](http://ww1.sinaimg.cn/large/006tNc79gy1g4zps9z05tj31b40rwjwa.jpg)

src/main/java/leetcode/_60_/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._60_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.getPermutation(3, 3));
10+
System.out.println(solution.getPermutation(4, 9));
11+
System.out.println(solution.getPermutation(3, 1));
12+
}
13+
}
14+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode._60_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public String getPermutation(int n, int k) {
8+
int[] factorial = new int[n + 1];
9+
List<Integer> leftNumList = new ArrayList<>();
10+
int sum = 1;
11+
factorial[0] = 1;
12+
for (int i = 1; i <= n; i++) {
13+
leftNumList.add(i);
14+
sum *= i;
15+
factorial[i] = sum;
16+
}
17+
StringBuilder stringBuilder = new StringBuilder();
18+
k--; // Amazing k--
19+
for (int i = 1; i <= n; i++) {
20+
int index = k / factorial[n - i];
21+
k = k % factorial[n - i];
22+
stringBuilder.append(leftNumList.get(index));
23+
leftNumList.remove(index);
24+
}
25+
return stringBuilder.toString();
26+
}
27+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### [60\. Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
The set `[1,2,3,...,_n_]` contains a total of _n_! unique permutations.
7+
8+
By listing and labeling all of the permutations in order, we get the following sequence for _n_ = 3:
9+
10+
1. `"123"`
11+
2. `"132"`
12+
3. `"213"`
13+
4. `"231"`
14+
5. `"312"`
15+
6. `"321"`
16+
17+
Given _n_ and _k_, return the _k_<sup>th</sup> permutation sequence.
18+
19+
**Note:**
20+
21+
* Given _n_ will be between 1 and 9 inclusive.
22+
* Given _k_ will be between 1 and _n_! inclusive.
23+
24+
**Example 1:**
25+
26+
```
27+
Input: n = 3, k = 3
28+
Output: "213"
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: n = 4, k = 9
35+
Output: "2314"
36+
```
37+
38+
39+
#### Solution
40+
41+
Language: **Java**
42+
43+
```java
44+
class Solution {
45+
   public String getPermutation(int n, int k) {
46+
       int[] factorial = new int[n + 1];
47+
       List<Integer> leftNumList = new ArrayList<>();
48+
       int sum = 1;
49+
       factorial[0] = 1;
50+
       for (int i = 1; i <= n; i++) {
51+
           leftNumList.add(i);
52+
           sum *= i;
53+
           factorial[i] = sum;
54+
      }
55+
       StringBuilder stringBuilder = new StringBuilder();
56+
       k--; // Amazing k--
57+
       for (int i = 1; i <= n; i++) {
58+
           int index = k / factorial[n - i];
59+
           k = k % factorial[n - i];
60+
           stringBuilder.append(leftNumList.get(index));
61+
           leftNumList.remove(index);
62+
      }
63+
       return stringBuilder.toString();
64+
  }
65+
}
66+
```
67+
![](http://ww1.sinaimg.cn/large/006tNc79gy1g4zps9z05tj31b40rwjwa.jpg)

0 commit comments

Comments
 (0)