File tree 3 files changed +97
-0
lines changed
solution/0000-0099/0060.Permutation Sequence
3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,40 @@ public class Solution {
207
207
}
208
208
```
209
209
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
+
210
244
### ** ...**
211
245
212
246
```
Original file line number Diff line number Diff line change @@ -186,6 +186,40 @@ public class Solution {
186
186
}
187
187
```
188
188
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
+
189
223
### ** ...**
190
224
191
225
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments