MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / get_permutation

Method get_permutation

60. Permutation Sequence/src/main.rs:8–20  ·  view source on GitHub ↗
(n: i32, k: i32)

Source from the content-addressed store, hash-verified

6
7impl Solution {
8 pub fn get_permutation(n: i32, k: i32) -> String {
9 let (mut n, mut k) = (n as usize, k as usize - 1);
10 let mut x: Vec<char> = (b'1'..).take(n).map(char::from).collect();
11 let mut buf = vec![];
12 loop {
13 let zone = Self::factorial(n - 1);
14 let choice = k / zone;
15 buf.push(x.remove(choice));
16 k -= choice * zone;
17 n -= 1;
18 if n == 0 { break buf }
19 } .iter().collect()
20 }
21
22 /// used for small num a: a!
23 pub fn factorial(n: usize) -> usize {

Callers

nothing calls this directly

Calls 3

removeMethod · 0.80
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected