From 96dfe011d62841294a99bf2c03f714b2f44c1117 Mon Sep 17 00:00:00 2001 From: Spxg Date: Sun, 15 Dec 2024 01:14:30 +0800 Subject: [PATCH] Add problem 1323: Maximum 69 Number --- src/lib.rs | 1 + .../iterative.rs | 41 +++++++++++++++++++ src/problem_1323_maximum_69_number/mod.rs | 18 ++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/problem_1323_maximum_69_number/iterative.rs create mode 100644 src/problem_1323_maximum_69_number/mod.rs diff --git a/src/lib.rs b/src/lib.rs index be21289..c6eed79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -537,6 +537,7 @@ pub mod problem_1304_find_n_unique_integers_sum_up_to_zero; pub mod problem_1310_xor_queries_of_a_subarray; pub mod problem_1315_sum_of_nodes_with_even_valued_grandparent; pub mod problem_1317_convert_integer_to_the_sum_of_two_no_zero_integers; +pub mod problem_1323_maximum_69_number; pub mod problem_1324_print_words_vertically; pub mod problem_1325_delete_leaves_with_a_given_value; pub mod problem_1333_filter_restaurants_by_vegan_friendly_price_and_distance; diff --git a/src/problem_1323_maximum_69_number/iterative.rs b/src/problem_1323_maximum_69_number/iterative.rs new file mode 100644 index 0000000..5094c28 --- /dev/null +++ b/src/problem_1323_maximum_69_number/iterative.rs @@ -0,0 +1,41 @@ +pub struct Solution; + +impl Solution { + pub fn maximum69_number(num: i32) -> i32 { + let mut result = 0; + let mut div = if num < 100 { + 10 + } else if num < 1000 { + 100 + } else { + 1000 + }; + let mut num = num; + let mut ok = false; + while num != 0 { + let mut digit = num / div; + num %= div; + div /= 10; + if digit == 6 && !ok { + ok = true; + digit = 9; + } + result = result * 10 + digit; + } + result + } +} + +impl super::Solution for Solution { + fn maximum69_number(num: i32) -> i32 { + Self::maximum69_number(num) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1323_maximum_69_number/mod.rs b/src/problem_1323_maximum_69_number/mod.rs new file mode 100644 index 0000000..d36a6a3 --- /dev/null +++ b/src/problem_1323_maximum_69_number/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn maximum69_number(num: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [(9669, 9969), (9996, 9999), (9999, 9999)]; + + for (num, expected) in test_cases { + assert_eq!(S::maximum69_number(num), expected); + } + } +}