From 25148752eab39a98e5e6aa31faa6f4756e4e3bde Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 11 Apr 2025 14:05:54 +0800 Subject: [PATCH] 20250411 finished. --- src/problem/mod.rs | 2 + src/problem/p2843_count_symmetric_integers.rs | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/problem/p2843_count_symmetric_integers.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 1e75299..7d6ee5d 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -591,3 +591,5 @@ mod p3396_minimum_number_of_operations_to_make_elements_in_array_distinct; mod p2999_count_the_number_of_powerful_integers; mod p3375_minimum_operations_to_make_array_values_equal_to_k; + +mod p2843_count_symmetric_integers; diff --git a/src/problem/p2843_count_symmetric_integers.rs b/src/problem/p2843_count_symmetric_integers.rs new file mode 100644 index 0000000..6789533 --- /dev/null +++ b/src/problem/p2843_count_symmetric_integers.rs @@ -0,0 +1,42 @@ +/** + * [2843] Count Symmetric Integers + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn count_symmetric_integers(low: i32, high: i32) -> i32 { + // 10 -> 99 1000 -> 9999 + (low..=high) + .filter_map(|i| { + if i >= 10 && i <= 99 { + if i / 10 == i % 10 { + return Some(()); + } + } + + if i >= 1000 && i <= 9999 { + if i / 1000 + (i / 100) % 10 == (i / 10) % 10 + i % 10 { + return Some(()); + } + } + + None + }) + .count() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2843() { + assert_eq!(9, Solution::count_symmetric_integers(1, 99)); + assert_eq!(4, Solution::count_symmetric_integers(1200, 1230)); + } +}