Skip to content

Commit 6545c5b

Browse files
committed
+ problem 60
1 parent 1d2defa commit 6545c5b

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 60. Permutation Sequence
2+
The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.
3+
4+
By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:
5+
6+
1. `"123"`
7+
2. `"132"`
8+
3. `"213"`
9+
4. `"231"`
10+
5. `"312"`
11+
6. `"321"`
12+
13+
Given `n` and `k`, return the <code>k<sup>th</sup></code> permutation sequence.
14+
15+
#### Example 1:
16+
<pre>
17+
<strong>Input:</strong> n = 3, k = 3
18+
<strong>Output:</strong> "213"
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> n = 4, k = 9
24+
<strong>Output:</strong> "2314"
25+
</pre>
26+
27+
#### Example 3:
28+
<pre>
29+
<strong>Input:</strong> n = 3, k = 1
30+
<strong>Output:</strong> "123"
31+
</pre>
32+
33+
#### Constraints:
34+
* `1 <= n <= 9`
35+
* `1 <= k <= n!`
36+
37+
## Solutions (Rust)
38+
39+
### 1. Solution
40+
```Rust
41+
impl Solution {
42+
pub fn get_permutation(n: i32, k: i32) -> String {
43+
let n = n as usize;
44+
let mut k = k as usize - 1;
45+
let mut digits = (b'1'..=(n as u8 + b'0')).collect::<Vec<_>>();
46+
let mut factorials = (0..n).collect::<Vec<_>>();
47+
let mut ret = vec![b'0'; n];
48+
49+
factorials[0] = 1;
50+
for i in 3..n {
51+
factorials[i] *= factorials[i - 1];
52+
}
53+
54+
for i in 0..n {
55+
ret[i] = digits.remove(k / factorials[n - i - 1]);
56+
k %= factorials[n - i - 1];
57+
}
58+
59+
String::from_utf8(ret).unwrap()
60+
}
61+
}
62+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 60. 排列序列
2+
给出集合 `[1,2,3,...,n]`,其所有元素共有 `n!` 种排列。
3+
4+
按大小顺序列出所有排列情况,并一一标记,当 `n = 3` 时, 所有排列如下:
5+
6+
1. `"123"`
7+
2. `"132"`
8+
3. `"213"`
9+
4. `"231"`
10+
5. `"312"`
11+
6. `"321"`
12+
13+
给定 `n``k`,返回第 `k` 个排列。
14+
15+
#### 示例 1:
16+
<pre>
17+
<strong>输入:</strong> n = 3, k = 3
18+
<strong>输出:</strong> "213"
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> n = 4, k = 9
24+
<strong>输出:</strong> "2314"
25+
</pre>
26+
27+
#### 示例 3:
28+
<pre>
29+
<strong>输入:</strong> n = 3, k = 1
30+
<strong>输出:</strong> "123"
31+
</pre>
32+
33+
#### 提示:
34+
* `1 <= n <= 9`
35+
* `1 <= k <= n!`
36+
37+
## 题解 (Rust)
38+
39+
### 1. 题解
40+
```Rust
41+
impl Solution {
42+
pub fn get_permutation(n: i32, k: i32) -> String {
43+
let n = n as usize;
44+
let mut k = k as usize - 1;
45+
let mut digits = (b'1'..=(n as u8 + b'0')).collect::<Vec<_>>();
46+
let mut factorials = (0..n).collect::<Vec<_>>();
47+
let mut ret = vec![b'0'; n];
48+
49+
factorials[0] = 1;
50+
for i in 3..n {
51+
factorials[i] *= factorials[i - 1];
52+
}
53+
54+
for i in 0..n {
55+
ret[i] = digits.remove(k / factorials[n - i - 1]);
56+
k %= factorials[n - i - 1];
57+
}
58+
59+
String::from_utf8(ret).unwrap()
60+
}
61+
}
62+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn get_permutation(n: i32, k: i32) -> String {
3+
let n = n as usize;
4+
let mut k = k as usize - 1;
5+
let mut digits = (b'1'..=(n as u8 + b'0')).collect::<Vec<_>>();
6+
let mut factorials = (0..n).collect::<Vec<_>>();
7+
let mut ret = vec![b'0'; n];
8+
9+
factorials[0] = 1;
10+
for i in 3..n {
11+
factorials[i] *= factorials[i - 1];
12+
}
13+
14+
for i in 0..n {
15+
ret[i] = digits.remove(k / factorials[n - i - 1]);
16+
k %= factorials[n - i - 1];
17+
}
18+
19+
String::from_utf8(ret).unwrap()
20+
}
21+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
[57][57l] |[Insert Interval][57] |![rb]&nbsp;&nbsp;![rs]
4949
[58][58l] |[Length of Last Word][58] |![rs]
5050
[59][59l] |[Spiral Matrix II][59] |![rs]
51+
[60][60l] |[Permutation Sequence][60] |![rs]
5152
[61][61l] |[Rotate List][61] |![py]
5253
[62][62l] |[Unique Paths][62] |![rs]
5354
[63][63l] |[Unique Paths II][63] |![rs]
@@ -1310,6 +1311,7 @@
13101311
[57]:Problemset/0057-Insert%20Interval/README.md#57-insert-interval
13111312
[58]:Problemset/0058-Length%20of%20Last%20Word/README.md#58-length-of-last-word
13121313
[59]:Problemset/0059-Spiral%20Matrix%20II/README.md#59-spiral-matrix-ii
1314+
[60]:Problemset/0060-Permutation%20Sequence/README.md#60-permutation-sequence
13131315
[61]:Problemset/0061-Rotate%20List/README.md#61-rotate-list
13141316
[62]:Problemset/0062-Unique%20Paths/README.md#62-unique-paths
13151317
[63]:Problemset/0063-Unique%20Paths%20II/README.md#63-unique-paths-ii
@@ -2565,6 +2567,7 @@
25652567
[57l]:https://leetcode.com/problems/insert-interval/
25662568
[58l]:https://leetcode.com/problems/length-of-last-word/
25672569
[59l]:https://leetcode.com/problems/spiral-matrix-ii/
2570+
[60l]:https://leetcode.com/problems/permutation-sequence/
25682571
[61l]:https://leetcode.com/problems/rotate-list/
25692572
[62l]:https://leetcode.com/problems/unique-paths/
25702573
[63l]:https://leetcode.com/problems/unique-paths-ii/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
[57][57l] |[插入区间][57] |![rb]&nbsp;&nbsp;![rs]
4949
[58][58l] |[最后一个单词的长度][58] |![rs]
5050
[59][59l] |[螺旋矩阵 II][59] |![rs]
51+
[60][60l] |[排列序列][60] |![rs]
5152
[61][61l] |[旋转链表][61] |![py]
5253
[62][62l] |[不同路径][62] |![rs]
5354
[63][63l] |[不同路径 II][63] |![rs]
@@ -1310,6 +1311,7 @@
13101311
[57]:Problemset/0057-Insert%20Interval/README_CN.md#57-插入区间
13111312
[58]:Problemset/0058-Length%20of%20Last%20Word/README_CN.md#58-最后一个单词的长度
13121313
[59]:Problemset/0059-Spiral%20Matrix%20II/README_CN.md#59-螺旋矩阵-ii
1314+
[60]:Problemset/0060-Permutation%20Sequence/README_CN.md#60-排列序列
13131315
[61]:Problemset/0061-Rotate%20List/README_CN.md#61-旋转链表
13141316
[62]:Problemset/0062-Unique%20Paths/README_CN.md#62-不同路径
13151317
[63]:Problemset/0063-Unique%20Paths%20II/README_CN.md#63-不同路径-ii
@@ -2565,6 +2567,7 @@
25652567
[57l]:https://leetcode.cn/problems/insert-interval/
25662568
[58l]:https://leetcode.cn/problems/length-of-last-word/
25672569
[59l]:https://leetcode.cn/problems/spiral-matrix-ii/
2570+
[60l]:https://leetcode.cn/problems/permutation-sequence/
25682571
[61l]:https://leetcode.cn/problems/rotate-list/
25692572
[62l]:https://leetcode.cn/problems/unique-paths/
25702573
[63l]:https://leetcode.cn/problems/unique-paths-ii/

0 commit comments

Comments
 (0)