Skip to content

Commit 0d35c05

Browse files
authored
feat: add rust solution to lc problem: No.60 (doocs#2106)
1 parent dd699e9 commit 0d35c05

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

solution/0000-0099/0060.Permutation Sequence/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,40 @@ public class Solution {
207207
}
208208
```
209209

210+
### **Rust**
211+
212+
```rust
213+
impl Solution {
214+
pub fn get_permutation(n: i32, k: i32) -> String {
215+
let mut k = k;
216+
let mut ans = String::new();
217+
let mut fact = vec![1; n as usize];
218+
for i in 1..n as usize {
219+
fact[i] = fact[i - 1] * (i as i32);
220+
}
221+
let mut vis = vec![false; n as usize + 1];
222+
223+
for i in 0..n as usize {
224+
let cnt = fact[(n as usize) - i - 1];
225+
for j in 1..=n {
226+
if vis[j as usize] {
227+
continue;
228+
}
229+
if k > cnt {
230+
k -= cnt;
231+
} else {
232+
ans.push_str(&j.to_string());
233+
vis[j as usize] = true;
234+
break;
235+
}
236+
}
237+
}
238+
239+
ans
240+
}
241+
}
242+
```
243+
210244
### **...**
211245

212246
```

solution/0000-0099/0060.Permutation Sequence/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,40 @@ public class Solution {
186186
}
187187
```
188188

189+
### **Rust**
190+
191+
```rust
192+
impl Solution {
193+
pub fn get_permutation(n: i32, k: i32) -> String {
194+
let mut k = k;
195+
let mut ans = String::new();
196+
let mut fact = vec![1; n as usize];
197+
for i in 1..n as usize {
198+
fact[i] = fact[i - 1] * (i as i32);
199+
}
200+
let mut vis = vec![false; n as usize + 1];
201+
202+
for i in 0..n as usize {
203+
let cnt = fact[(n as usize) - i - 1];
204+
for j in 1..=n {
205+
if vis[j as usize] {
206+
continue;
207+
}
208+
if k > cnt {
209+
k -= cnt;
210+
} else {
211+
ans.push_str(&j.to_string());
212+
vis[j as usize] = true;
213+
break;
214+
}
215+
}
216+
}
217+
218+
ans
219+
}
220+
}
221+
```
222+
189223
### **...**
190224

191225
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl Solution {
2+
pub fn get_permutation(n: i32, k: i32) -> String {
3+
let mut k = k;
4+
let mut ans = String::new();
5+
let mut fact = vec![1; n as usize];
6+
for i in 1..n as usize {
7+
fact[i] = fact[i - 1] * (i as i32);
8+
}
9+
let mut vis = vec![false; n as usize + 1];
10+
11+
for i in 0..n as usize {
12+
let cnt = fact[(n as usize) - i - 1];
13+
for j in 1..=n {
14+
if vis[j as usize] {
15+
continue;
16+
}
17+
if k > cnt {
18+
k -= cnt;
19+
} else {
20+
ans.push_str(&j.to_string());
21+
vis[j as usize] = true;
22+
break;
23+
}
24+
}
25+
}
26+
27+
ans
28+
}
29+
}

0 commit comments

Comments
 (0)