20250608 finished.

This commit is contained in:
jackfiled 2025-06-08 15:38:26 +08:00
parent f1d54e2540
commit 7a5716233b
Signed by: jackfiled
GPG Key ID: DEF448811AE0286D
2 changed files with 52 additions and 0 deletions

View File

@ -698,3 +698,5 @@ mod p1061_lexicographically_smallest_equivalent_string;
mod p2434_using_a_robot_to_print_the_lexicographically_smallest_string;
mod p3170_lexicographically_minimum_string_after_removing_stars;
mod p386_lexicographical_numbers;

View File

@ -0,0 +1,50 @@
/**
* [386] Lexicographical Numbers
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn lexical_order(n: i32) -> Vec<i32> {
let mut result = Vec::with_capacity(n as usize);
for i in 1..10 {
Self::search(&mut result, i, n);
}
result
}
fn search(result: &mut Vec<i32>, mut prefix: i32, n: i32) {
if prefix <= n {
result.push(prefix);
}
prefix = prefix * 10;
for i in 0..10 {
if prefix + i > n {
break;
}
Self::search(result, prefix + i, n);
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_386() {
assert_eq!(vec![1, 2], Solution::lexical_order(2));
assert_eq!(
vec![1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],
Solution::lexical_order(13)
);
}
}